Reflection : Different behavior while using typeof() or IsAssignableFrom() to check interface implemented by a class

StackOverflow https://stackoverflow.com/questions/20699595

  •  20-09-2022
  •  | 
  •  

Question

I have two interfaces IConfigurationManager and ILocator defined in the same assembly (lets call it assembly "A") and the same namespace.

There is a start-up project called "B" which has "project reference" to the assembly "A".

Assembly A contains concrete implementations of both the interfaces too. I have a "post-build event" on assembly A which copies DLL to the "B" project's 'bin\debug' folder from where application is run when i run it from visual studio 2012.

In the app.config file, I mention which assembly and type to use when I want concrete implementations of both the interfaces. (I need to resolve type mapping at run time and will have interfaces and implementations in different assemblies which I many not develope at all.)

In my code, I load assembly A (using Assembly.LoadFile()) from the 'bin\debug\' folder of the start-up project. Then I iterate through all types and check if a class is of type ILocator using this code:

if(type.GetInterfaces.Contains(typeof(IConfigurationManager))

This returns FALSE but on the other hand, similar code for the ILocator returns TRUE. Why does this happen? I tried to use IsAsseignableFrom() too, but I got the same result.

Why one call succeeds and other fails when everything (like assembly in which interfaces declared, assembly from which type is loaded etc.) else is same while checking interfaces implemented by a class?

Note: I am aware of the Unity container etc. But I still have the same question related to type checking using reflection.

Was it helpful?

Solution

Probably because the 'everything else' is not the same. Since your B project has a project reference to A, it ends up loading A twice: first by CLR, when B uses the type ILocator from A, and then when you explicitly load A with LoadFile. Because LoadFile loads assemblies into a different binding context, you get two copies of A in your application domain, with two sets of types which (confusingly) have the same FullName etc. but are nevertheless different. You may check this by comparing the Assembly objects in the IConfigurationManager from type.GetInterfaces () (or type for that matter) and from typeof (IConfigurationManager).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top