Type .GetType (string typeName) returns null

My code

type = Type.GetType(key); 

The key I am passing is the qualified namespace name.

My code is in BusinessLayer. I am creating an instance of DataAccessLayer. The DataAccessLayer link is added to the BusinessLayer.

I get an error message like "Could not load type" Catalyst.DAL.ExamDAO.CExamDAO "from the assembly" BusinessLayer, Version = 1.9.3.0, Culture = neutral, PublicKeyToken = null " .

What should I do to explicitly indicate that a class belongs to a DataAccessLayer?

The key key is "Catalyst.DAL.ExamDAO.CExamDAO"

Edit:

My actual code

 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 default classes if there is no setting. The method is in BO. If I pass the key as a namespace, then qualified names of any Bo type that it converts. But DAO type it wont

+6
source share
5 answers

If you know that no matter what type it is, it will be within the DataAccessLayer , then I would get the Assembly link as simple as possible, for example.

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

An alternative is to use Type.GetType with the name defined by the assembly, but longer in terms of specifying the type name.

+3
source

If the type is not in the assembly call, you need to use AssemblyQualifiedName to get an instance of Type. To solve the problem, you need to set the key value to AssemblyQualifiedName instead of namespace qualified name .

+3
source

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

 Catalyst.DAL.ExamDAO+CExamDAO 

The best you can do is to create CExamDAO directly and then take its GetType().AssemblyQualifiedName (for example, in the debugger). Sort of:

 (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 into your code.

0
source

Is your type publicly available? Inner classes cannot be loaded from different assemblies.

0
source

Or try the following:

  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; } 

Using:

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

Call the static ToList method in the StringHelper class in the Utility namespace in the DaProject project (the same assembly and project name).

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

0
source

Source: https://habr.com/ru/post/897450/


All Articles