加载 .DLL 文件并从类中访问方法? [英] Load a .DLL file and access methods from class within?
问题描述
我对加载这样的库完全陌生,但我的立场是:
I'm completely new to loading in libraries like this, but here's where I stand:
我有一个自制的 DLL 文件,它非常简单,包括类本身和方法.在加载这个库的主程序中,我有:
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?
谢谢.
推荐答案
使用 Assembly.GetTypes()
获取所有类型的集合,或 Assembly.GetType(name)
获取特定类型.
Use Assembly.GetTypes()
to get a collection of all the types, or Assembly.GetType(name)
to get a particular type.
然后,您可以使用 Activator.CreateInstance(type)
使用无参数构造函数创建该类型的实例,或使用 Type.GetConstructors
获取构造函数并调用它们以创建实例.
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.
同样,您可以使用 Type.GetMethods()
等获取方法
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.
这篇关于加载 .DLL 文件并从类中访问方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!