简体   繁体   中英

Finding base type of types loaded from reflection

I have a basic scenario.

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;
    }
}

In this case no match found. But if I add reference of that assembly and call this method again.

bool matchFound = typeof(MyDerivedClass).IsSubClassOf(typeof(MyBaseClass));

matchFound is true in this case.

I want first case to be running. Any suggestion

Assemblies loaded via LoadFrom are not loaded into the default assembly context. This can cause problems like yours. Try using Assemlby.Load instead. Here are a few links for more info´s that could be usefull

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM