控制台程序示例。
class Program
{
static void Main(string[] args)
{
// ... code to build dll ... not written yet ...
Assembly assembly = Assembly.LoadFile(@"C:\dyn.dll");
// don't know what or how to cast here
// looking for a better way to do next 3 lines
IRunnable r = assembly.CreateInstance("TestRunner");
if (r == null) throw new Exception("broke");
r.Run();
}
}
我想动态生成一个程序集(。Dll) ,然后加载程序集,实例化一个类,并调用该类的 Run ()方法。我是否应该尝试将 TestRunner 类转换为其他类?不确定一个程序集(动态代码)中的类型如何知道我的(静态程序集/shell 应用程序)中的类型。仅仅使用几行反射代码在一个对象上调用 Run ()是否更好?代码应该是什么样子的?
更新: William Edmondson-参见评论