具有完全相同的assemblyQualifiedName但不相等的两个Type

时间:2019-07-03 06:38:52

标签: c#

我正在实现一个非常简单的事件处理程序,该事件处理程序使用反射从程序集中加载消息句柄类。

当我收到一些消息时,我尝试从加载的程序集中解析句柄类。并找出哪个类可以处理消息。

但是当我比较消息类型和句柄接口类型时,它总是返回false。

对于这种消息类型,我只有一个工具。

    foreach(var @interface in info.GetInterfaces())
    {
        if (@interface.IsGenericType)
        {
            if (@interface.GetGenericTypeDefinition() ==
                typeof(IAsyncMessageSubscriber<>))
                {
                    var arg = @interface.GenericTypeArguments[0];
                    arg.Assembly
                    if (arg == messageType)
                    {
                        return true;
                    }
                }
        }
    }

debug details

1 个答案:

答案 0 :(得分:0)

好吧,问题是我使用了Assembly.LoadFrom而不是Assembly.Load。 Assembly.Load从程序集和类型加载到另一个appdomain中,该类型使该类型与当前appdomain中的不相等。 使用Assembly.Load可以解决此问题。