从class中加载一个.DLL文件和访问方法? [英] Load a .DLL file and access methods from class within?
问题描述
我有一个自制的DLL文件,就像它得到的一样简单,该类本身和一种方法。在加载该库的家庭程序中,我有:
程序集testDLL = Assembly.LoadFile(C:\\ dll\\test.dll);
从这里,我有点困惑。据我所知,它正确加载,因为当我更改名称时给我错误。
我从这里做什么?我如何加载班级&其中的方法?
谢谢。
使用 .GetTypes()
来获取所有类型的集合,或者获取一个特定类型的 Assembly.GetType(name)
然后,您可以使用 Activator.CreateInstance(type)
使用无参数构造函数创建类型的实例,或者使用 Type.GetConstructors
并调用它们来创建实例。
同样,您可以使用类型.GetMethods()
等。
基本上,一旦你有一个类型,有很多事情你可以做 - 看看<有关详细信息,请参阅href =http://msdn.microsoft.com/en-us/library/system.type_members.aspx =noreferrer>成员列表。如果你坚持试图执行一个特定的任务(泛型可能是棘手的)只是问一个具体的问题,我相信我们将能够帮助。
I'm completely new to loading in libraries like this, but here's where I stand:
I have a homemade DLL file it's about as simple as it gets, the class itself and a method. In the home program that loads this library, I have:
Assembly testDLL = Assembly.LoadFile("C:\\dll\\test.dll");
From here, I'm kind of stuck. As far as I know, it's loading it correctly because it gives me errors when I change the name.
What do I do from here? How exactly do I load the class & methods within it?
Thanks.
Use Assembly.GetTypes()
to get a collection of all the types, or Assembly.GetType(name)
to get a particular type.
You can then create an instance of the type with a parameterless constructor using Activator.CreateInstance(type)
or get the constructors using Type.GetConstructors
and invoke them to create instances.
Likewise you can get methods with Type.GetMethods()
etc.
Basically, once you've got a type there are loads of things you can do - look at the member list for more information. If you get stuck trying to perform a particular task (generics can be tricky) just ask a specific question an I'm sure we'll be able to help.
这篇关于从class中加载一个.DLL文件和访问方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!