Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type.GetType(string typeName) returns null

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

like image 296
Kuntady Nithesh Avatar asked Sep 16 '11 06:09

Kuntady Nithesh


People also ask

Can GetType return null?

If typeName cannot be found, the call to the GetType(String) method returns null . It does not throw an exception.

What is return type of GetType ()?

The gettype() function returns the type of a variable.

What is GetType C#?

GetType Method is used to find the type of the current instance. This method returns the instances of the Type class that are used for consideration. Syntax: public Type GetType ();


2 Answers

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.

like image 139
Bharath Avatar answered Sep 22 '22 13:09

Bharath


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.

like image 28
Jon Skeet Avatar answered Sep 24 '22 13:09

Jon Skeet