16
votes

I'm trying to debug some code that uses reflection to load plugins

Here's the debugging code:

Type a = methodInfo.GetParameters()[0]
    .ParameterType.BaseType;
Type b = typeof(MessageContext);
Debug.WriteLine(a.AssemblyQualifiedName);
Debug.WriteLine(b.AssemblyQualifiedName);
Debug.WriteLine(a.Equals(b));

And here is its output:

OrtzIRC.Common.MessageContext, OrtzIRC.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
OrtzIRC.Common.MessageContext, OrtzIRC.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
False

I don't understand what would make these two types different?

4
What does .UnderlyingSystemType return on both of them?Noon Silk
Well then. Were they loaded by different class loaders?Noon Silk
What I mean is, do you have a small stand-alone runnable sample that we can test with?Noon Silk

4 Answers

21
votes

The same class / type loaded by different app domains [.NET] or class loaders [Java] will not compare equal and are not assignable to/from each other directly.

You likely have two copies of the DLL containing that type - one loaded by the main program and one loaded by one of the Assembly.Load*(...) methods?

Try displaying / comparing the properties:
a.Assembly.Equals(b.Assembly)
and
a.Assembly.Location.Equals(b.Assembly.Location)

In general, you only want one copy of each DLL and have it loaded into a single app domain.

3
votes

This can happen if the two types are loaded from different versions of the assembly. .NET considers them different, unrelated types. Check

Debug.WriteLine (a.AssemblyQualifiedName) ;
Debug.WriteLine (b.AssemblyQualifiedName) ;
0
votes

Try: Debug.Writeline(a.Equals(b));

0
votes

This is how you should compare the types:

C# Object Type Comparison

I think your problem resides in the type hierarchy...