我试图找到一种方法,在我的应用程序启动后,我可以从.NET DLL执行代码.我在下面包含了一些伪代码,可能会尝试解释我正在尝试做什么.我知道这可能比我看起来要复杂得多.
ClassLibrary myLibrary = new ClassLibrary("C:\\Users\\Admin\\Desktop\\myTestLibrary.dll");
myLibrary.executeMethod("showMessageMethod", arg1, arg2, arg3...);
Run Code Online (Sandbox Code Playgroud)
这就是我想要做的事情,虽然我知道它可能远比那复杂!
我还要说清楚,我确实理解你的目的是在你的项目中引用库...但是我的项目要求我不这样做.
提前致谢!
Ste*_*ven 12
您需要从磁盘加载程序集,如下所示:
Assembly myLibrary = System.Reflection.Assembly
.LoadFile("C:\\Users\\Admin\\Desktop\\myTestLibrary.dll");
Run Code Online (Sandbox Code Playgroud)
之后,您需要使用反射获取正确的类型并调用正确的方法.当您要调用的类实现在启动时引用的程序集中定义的接口时,这将是最方便的:
Type myClass = (
from type in myLibrary.GetExportedTypes()
where typeof(IMyInterface).IsAssignableFrom(type)
select type)
.Single();
var instance = (IMyInterface)Activator.CreateInstance(myClass);
instance.executeMethod("showMessageMethod", arg1, arg2, arg3...);
Run Code Online (Sandbox Code Playgroud)