How to call a function in DLL.net through an interface loaded via reflection

Hello, I will try to explain this.

Basically, I load the library through reflection using Assembly.LoadFile.

From there, I have an IFace interface that defines a GetStrings method that returns an array of strings.

A dynamically loaded DLL has a class called Class1 that implements IFace.

I need a way to call this conjugate method through a dynamically loaded lib library. I would like to keep it tightly bound, which leaves me wondering what to do. I know that I can use MethodInvoker to call a method, but I'm trying to find a way that I can do something like this:

IFace obj = (IFace)ReflectionAssembly.Class1;
string[] strs = obj.GetStrings();

Any ideas?

+3
source share
3

- :

    var assm = Assembly.Load("ClassLibrary1");
    var type = assm.GetType("ClassLibrary1.Class1");
    var instance = Activator.CreateInstance(type) as IFace;
    string[] strings = instance.GetStrings();
+5

Assembly.CreateInstance() , " ". IFace, . , LoadFile, LoadFrom.

+2

Once you have Type via Reflection (using something like Assembly.GetType ), you can use Activator.CreateInstance :

IFace obj = (IFace)Activator.CreateInstance(class1Type);
string[] strs = obj.GetStrings();
+1
source

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


All Articles