MetadataLoadContext and inheritance

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,678
Location
Norway
Programming Experience
10+
I'm using MetadataLoadContext to inspect assemblies and wonder if it is possible to detect inheritance for a type? Regular reflections methods IsSubclassOf or IsAssignableFrom does not work. Specifically I would like to check if a type is a delegate without having to do string comparison on the BaseType.Name.

 
Interesting... So this statement in that link you provided is incorrect?
You can use all reflection APIs on loaded assemblies except ones that involve code execution.

I would not have expected IsSubclassOf() to need to execute code. I can sort of see how IsAssignableFrom() would need to execute code due to implicit and explicit overrides that can be written by a class author.
 
Oh, I see... At the bottom of the documentation for IsSubclassOf():
Note
Except when used with interfaces, IsSubclassOf is the converse of IsAssignableFrom. That is, if t1.IsSubclassOf(t2) is true, then t2.IsAssignableFrom(t1) is also true.
This method can be overridden by a derived class.
 
No type comparisons seems to work with the "meta types", but it is very fast compared to regular reflection that is known to be slow.
For example in reflection-only context CustomAttributesData.AttributeType would compare for example with TargetFrameworkAttribute, doing the same with MetadataLoadContext will compare as false - but one can compare AttributeType.Name with string "TargetFrameworkAttribute".
 
Have you taken a peek to see how MEF does things to determine whether an add-in is supported or not? Or does it just rely on plain old reflection by loading the assembly?
 
MEF uses plain reflection. I'm not trying to load addins, just discovering assembly information. I have this some kind of working now.
 
Back
Top Bottom