c# 强名称程序集反射加载接口转换问题

c# strong name assembly reflection load interface cast problem

我已经隔离了我用于分布式测量和数据解释的以下问题(当 impl 知道如何处理数据及其示例中不存在的 UserControl 时)。

我在解决方案中有 3 个不同的项目: 1)接口(单独的dll)

namespace inter
{
    public interface IInter
    {
        byte[] results();
    }
}

2) 实施 class(单独的 dll)

using inter;

namespace impl
{
    public class impl : IInter
    {
        public byte[] results()
        {//dummy data production
            return new byte[10].Select((ii, index) => (byte)index).ToArray();
        }
    }
}

3) 运行问题的程序...

using inter;
using impl;

namespace test_class_casting
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = @"C:\Users\pavel\source\repos\test class casting\impl\bin\Debug\impl.dll";
            var b = File.ReadAllBytes(path);
            var assembly = Assembly.Load(b);

            IInter inst = null;
            var expTypes = assembly.GetExportedTypes();
            foreach (var d in assembly.GetExportedTypes())
            {
                var obj = Activator.CreateInstance(d);
                if (obj is IInter)
                {
                    inst = obj as IInter;
                    try
                    {
                        var res = inst.results();
                        continue;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.ReadKey();
                        Environment.Exit(0);
                    }
                }
            }

            try
            {
                var reType = (impl.impl)inst;// as impl.impl;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                Environment.Exit(0);
            }

            Console.WriteLine("...");
            Console.ReadKey();
        }
    }
}

问题是重新类型转换将失败,因为 [A]impl.impl 无法转换为 [B]impl.impl. 异常消息。 我怎样才能让 wotk 脚本识别两种 class 类型是相同的。(注意。我不能 link 引用到项目中,实现 impl class这些都是用户的责任,所以我都不知道。

我什至尝试为 inter 和 impl 添加一个强名称,但现在取得了进展。你有解决办法吗?

毕竟,经过一些程序集研究,我找到了解决方案。每次动态加载程序集时,代码都可以检查它是否已经在 AppDomain.CurrentDomain.GetAssemblies() 中(通过 FullName 属性 检查)。 IE。如果程序集引用了项目。

如果 none 存在,则使用新程序集并创建其 GetExportedTypes() 中的实例。

如果已经存在,则已经加载的用于创建实例。示例代码在这里 https://www.dropbox.com/s/hbs0ndwh8cf3uq0/test%20class%20casting-resolved.zip?dl=0