Home > Software design >  Error using reflection, when trying to get the type
Error using reflection, when trying to get the type

Time:11-26

I am trying to use reflection.

Here is my function using reflection:

name = "IfcPolyLoop" polyLoopLine="#99= IFCPOLYLOOP((#101,#103,#105,#107));"

    private void convertFirstIfcPolyLoop(string typeName,string polyLoopLine)
    {
        var type = Type.GetType(typeName);
        object instance = Activator.CreateInstance(type, polyLoopLine, this.listDictionaries);
        this.AddOrError((IfcElement)instance);
    }

What I want to do is something equivalent to that :

private void convertFirstIfcPolyLoop(string polyLoopLine)
{
    IfcPolyLoop newElt = new IfcPolyLoop(polyLoopLine, this.listDictionaries)
}

But I want to use reflection.

But when I do var type = Type.GetType(typeName);, type is still set to null, and I think I am supposed to obtain IfcPolyLoop

CodePudding user response:

Although the classes are in the same namespaces, you need to provide the class name along with the namespace.

So for example you need to pass YourNamespace.YourClassName as the string value to the Type.GetType method.

In my case, it worked when passing as "ConsoleApp1.Program2" for the class Program2 in ConsoleApp1 namespace.

  • Related