Type.GetType(string typeName) returns null
Asked Answered
P

5

6

My code is

type = Type.GetType(key);

Key which i pass is a namespace qualified name .

My code is in BusinessLayer. I am creating a instance of DataAccessLayer. DataAccessLayer reference is added to BusinessLayer.

I am getting the error as "Could not load type 'Catalyst.DAL.ExamDAO.CExamDAO' from assembly 'BusinessLayer, Version=1.9.3.0, Culture=neutral, PublicKeyToken=null'.".

What should i do to specify explicitly thats the class is from DataAccessLayer ?

Key vale is "Catalyst.DAL.ExamDAO.CExamDAO"

Edit :

My actual code is

public static object getClassInstance(string key, params  object[] constructorArgs)
        {
            string assemblyPath = null;
            string customClassName = null;
            DataSet objDataset = getAssemblyInfo(key);
            if (objDataset != null && objDataset.Tables.Count > 0 && objDataset.Tables[0].Rows.Count > 0)
            {
                assemblyPath = objDataset.Tables[0].Rows[0]["ACA_ASSEMBLY_PATH"].ToString();
                customClassName = objDataset.Tables[0].Rows[0]["ACA_CLASS_NAME"].ToString();
            }

            Assembly assembly;
            Type type;

            if (assemblyPath != null && assemblyPath != string.Empty)
            {
                assembly = Assembly.LoadFile(assemblyPath);
                type = assembly.GetType(customClassName);
            }
            else // if no customisation
            {
                type = Type.GetType(key);
            }

            object classInstance = constructorArgs == null ? Activator.CreateInstance(type) : Activator.CreateInstance(type, constructorArgs);
            if (classInstance == null) throw new Exception("broke");
            return classInstance;

        }

I am trying to load the default classes if there is no customization . Method is in BO . If i pass the key as namespace qualified names of any Bo type it converts . But DAO type it wont

Player answered 16/9, 2011 at 6:36 Comment(5)
Why are you doing it this way instead of new CExamDAO()? if it is true that "DataAccessLayer reference is added to BusinessLayer" then should be no reason you can't use the new operator. Or if you really just want a type reference then typeof(CExamDAO)Legitimate
Give us the text value of "key".Wakerobin
@Adam Ralph . Actually i need to switch between custom assemblies and default assembly . I dint put that code here .Player
@Wakerobin i put the key valuePlayer
possible duplicate of Type.GetType("namespace.a.b.ClassName") returns nullCroup
M
3

If you know that whatever type it is will be within DataAccessLayer, then I'd get an Assembly reference as simply as possible, e.g.

 Assembly assembly = typeof(AnyPublicTypeWithinTargetAssembly).Assembly;
 Type type = assembly.GetType(namespaceQualifiedTypeName);

An alternative is to use Type.GetType with an assembly-qualified name, but that's more long-winded in terms of specifying the type name.

Matrix answered 16/9, 2011 at 6:38 Comment(2)
what is AnyPublicTypeWithinTargetAssembly , I am not getting access to this.Which assembly reference i need to use.Player
AnyPublicTypeWithinTargetAssembly is any public type withn target assembly. Just pick one type in that assembly that you will not remove later, and put it in the expression. Also if you know the name of the assembly you could use Assembly.LoadFrom(assemblyName)Kosygin
C
3

If the type is not present in calling assembly you need to use the AssemblyQualifiedName to get it Type instance. To resolve your issue, you need set key value with AssemblyQualifiedName instead of namespace qualified name.

Crowded answered 16/9, 2011 at 7:11 Comment(1)
This is an important point, not mentioned in the selected answer: if the specified type is not from the calling assembly, the Assembly Qualified name needs to be used. It is not enough to just use the assembly name.Keenan
W
0

If CExamDAO is a subclass of ExamDao, then the notation is (note the +):

Catalyst.DAL.ExamDAO+CExamDAO

The best thing you can do is create a CExamDAO directly and then take its GetType().AssemblyQualifiedName (for example in the debugger). Something like:

(new CExamDAO()).GetType().AssemblyQualifiedName

or (if you are sure where you need it its assembly is already loaded)

(new CExamDAO()).GetType().FullName

and then copy/paste it in your code.

Wakerobin answered 16/9, 2011 at 7:10 Comment(0)
E
0

Is your type public? Internal classes can not be loaded from different assemblies.

Eunuchoidism answered 16/9, 2011 at 7:12 Comment(0)
S
0

Or try this:

 private static object GetResultFromStaticMethodClass(string qualifiedClassName, string method)
 {
      Type StaticClass = Type.GetType(qualifiedClassName);
      MethodInfo methodInfo = StaticClass.GetMethod(method);
      object result = methodInfo.Invoke(null, null);
      return result;
 }

Use:

object result = GetResultFromStaticMethodClass(
    "Utilities.StringHelper,DaProject",
    "ToList"
);

This call the static method ToList in the StringHelper class, in the Utilities namespace, in the DaProject project (same assembly and project name).

If you need parameters, add them in the second parameter in the methodInfo.Invoke(null, null) call

Severance answered 11/4, 2013 at 6:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.