如何从加载的DLL管理类? [英] How to manage classes from a loaded DLL?
问题描述
如何将类转换成一个变量来调用函数并获取属性?
我被告知要查看反射,但是当DLL未知时,我不知道如何完成编译时间。
How can I get a class into a variable to call function and get properties?
I have been told to look into reflection but I don't get how it's done when the DLL was not known at compile time.
要清楚:我有一个'main'类加载一个DLL,我想在DLL中创建一个类的实例,并直接获取属性
To be clear: I have a 'main' class which loads an DLL and I want to create an instance of a class within the DLL and directly get properties.
推荐答案
你应该这样做:
Assembly asm = Assembly.Load("DLL File Path"); //load DLL from file
Type t = asm.GetType("Test.ExternalDllTest"); //fully qualified name
dynamic oDynamic = Activator.CreateInstance(t, args);//create an instance of specified type, and assign it to DYNAMIC object
编辑
oDynamic.SomeMethod(); //call the method of your type. Being DYNAMIC it will route the call to correct method.
自然地, DLL
必须是托管DLL (所以写在 .NET
语言)
Naturally, the DLL
has to be a managed DLL (so written in .NET
language)
我没有编译这个老实说,但基本上这是一个如何做的想法。
I didn't compile this, honestly, but basically this is an idea of how to do that.
这里还有一个示例,这可能有帮助。
Here also an example, that may help.
这篇关于如何从加载的DLL管理类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!