两种类型不相等,应该是 [英] Two Types not equal that should be
问题描述
我正在尝试调试一些使用反射加载插件的代码
I'm trying to debug some code that uses reflection to load plugins
调试代码如下:
Type a = methodInfo.GetParameters()[0]
.ParameterType.BaseType;
Type b = typeof(MessageContext);
Debug.WriteLine(a.AssemblyQualifiedName);
Debug.WriteLine(b.AssemblyQualifiedName);
Debug.WriteLine(a.Equals(b));
这是它的输出:
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
错误
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?
推荐答案
由不同的应用程序域 [.NET] 或类加载器 [Java] 加载的相同类/类型不会比较相等,也不能相互分配直接.
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.
您可能有两份包含该类型的 DLL - 一份由主程序加载,一份由 Assembly.Load*(...)
方法之一加载?
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?
尝试显示/比较属性:a.Assembly.Equals(b.Assembly)
和a.Assembly.Location.Equals(b.Assembly.Location)
通常,您只需要每个 DLL 的一个副本并将其加载到单个应用程序域中.
In general, you only want one copy of each DLL and have it loaded into a single app domain.
这篇关于两种类型不相等,应该是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!