我有一个基本情景。
Assembly asm = Assembly.LoadFrom(dllPath);
Type[] temp = asm.GetTypes();
bool matchFound = false;
foreach (Type t in temp)
{
if (t.IsClass && t.IsSubClassOf(typeof(MyBaseClass)))
{
Console.WriteLine("Match found");
matchFound = true;
break;
}
}
在这种情况下找不到匹配项。但是如果我添加该程序集的引用并再次调用此方法。
bool matchFound = typeof(MyDerivedClass).IsSubClassOf(typeof(MyBaseClass));
在这种情况下,matchFound为真。
我想要第一个案例正在运行。任何建议
答案 0 :(得分:0)
通过LoadFrom
加载的程序集不会加载到默认程序集上下文中。这可能会导致像你这样的问题。请尝试使用Assemlby.Load
。
以下是一些可能有用的更多信息链接
http://gotoanswer.com/?q=Loading+a+class+from+an+assembly+dynamically+%28C%23%29
Reflection not working on assembly that is loaded using Assembly.LoadFrom
https://msdn.microsoft.com/en-us/library/ms172214.aspx
http://geekswithblogs.net/rupreet/archive/2010/02/16/137988.aspx