开发者

Different behavior of IsSubClassOf for referenced type and loaded types

I'm seeing a different behavior when I'm checking subclasses (Abstract->deriv) for type when I use the "base" type that is referenced in the checker program compared to when I explicitly load the "base" type in the checker program.

So if I do this in my main Checker program which has base type referenced (baseType) in:

  1. Load the derivType as Assembly.ReflectionOnlyLoadFrom(Path_to_deriv_dll)->typeToLoad
  2. var baseType = typeof(abstract_class) //Assembly for abstract_class is referenced in
  3. typeToLoad.IsSubClassOf(baseType) yields False

However if I do this:

  1. Load the baseType as Assembly.ReflectionOnlyLoadFrom(Path_to_base_type_dll)->baseType
  2. Load the derivType as Assembly.ReflectionOnlyLoadFrom(Path_to_deriv_dll)->typeToLoad
  3. Check typeToLoad.IsSubClassOf开发者_如何学JAVA(baseType), it yields as True

Any thoughts are appreciated.


The reason for this in your case is that the type have different identities. When using LoadFrom the assembly is loaded into the LoadFrom context and vs. the references which are loaded into the Load context. Because the assemblies are loaded into different contexts the types in the assemblies have different identities.

Here are some links with some more info on the subject http://blogs.msdn.com/b/suzcook/archive/0001/01/01/57248.aspx http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx


Make sure that the version of the assembly being referenced in the first option is the same version as the assembly being loaded dynamically in the second option. Provided you're loading the same exact assembly in both cases (same version, etc), you should get the same answer.

If, however, you are referencing a different version of the assembly, you may get a different answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜