Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Types not equal that should be

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?

like image 621
Brian Ortiz Avatar asked Sep 02 '10 02:09

Brian Ortiz


People also ask

How do you know if types are equal?

Equals() Method is used to check whether the underlying system type of the current Type is the same as the underlying system type of the specified Object or Type.

Why is an object not equal to an object?

Objects are not compared by value: two objects are not equal even if they have the same properties and values. This is true of arrays too: even if they have the same values in the same order. Objects are sometimes called reference types to distinguish them from JavaScript's primitive types.

How to give Not equal to in c#?

Inequality operator !=returns true if its operands are not equal, false otherwise. For the operands of the built-in types, the expression x != y produces the same result as the expression !( x == y) .


1 Answers

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.

like image 155
Jorgen Thelin Avatar answered Sep 24 '22 03:09

Jorgen Thelin