Anirban Ghosh
Anirban Ghosh

Reputation: 125

Creating "Type" object of a class by Type.getType

I am stuck here. In my vs solution we have 10 different projects. DAL contains 2 EF 6.0 objects, Say EG and EL. I have an API project. In which I have a function as follows, I am trying to return properties of an EF class:

public IEnumerable<PropertyInfo> GetGetProperties(string className, string instanceType)
    {
        Type thisType;

        if (instanceType.Contains("G"))
        {
            thisType = Type.GetType("E.DAL.EG." + className);
        }
        else 
        {
            thisType = Type.GetType("E.DAL.EL." + className);
        }            
        return thisType.GetType().GetProperties();
    }

But every time "thisType" returns null. Is this because of the AssemblyInformation? if so what would be the correct code. I have Assembly's fully qualified name in string. Thanks-- Anirban

Upvotes: 0

Views: 224

Answers (1)

Paweł Łukasik
Paweł Łukasik

Reputation: 4153

The problem is the last line. You are calling .GetType() on type Type (variable thisType) and that would return RuntimeType and not your type. The last line should be

return thisType.GetProperties();

and if your namespaces are correct, so you do have those namespaces (E.DAL.EL and E.DAL.EG) in the project, it will work.

If it doesn't check the namespaces and verify that those types are in the same assembly as your code! If they are in an external one you need to specify it for the GetType method.

Assuming those are in assembly named SharedAssembly then you need to load them this way

thisType = Type.GetType("E.DAL.EG." + className+ ", SharedAssembly");

Upvotes: 1

Related Questions