Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

后端 未结 13 2315
-上瘾入骨i
-上瘾入骨i 2020-11-22 17:21

Is it possible to instantiate an object at runtime if I only have the DLL name and the class name, without adding a reference to the assembly in the project? The class imple

13条回答
  •  天涯浪人
    2020-11-22 17:45

    Assembly assembly = Assembly.LoadFrom("MyAssembly.dll");
    
    Type type = assembly.GetType("MyType");
    
    dynamic instanceOfMyType = Activator.CreateInstance(type);
    

    So in this way you can use functions not with getting methodinfo,and then invoking it.You will do like this instanceOfMyType.MethodName(); But you can't use Intellisense because dynamic types are typed in runtime,not in compile time.

提交回复
热议问题