Mono 无法将 TypeA 转换为 TypeA

本文关键字:TypeA 转换 Mono | 更新日期: 2023-09-27 18:36:49

我正在用C#创建一个插件框架。框架的主要要求是在运行时加载、卸载和更新插件。

为了实现这一点,我一直在创建应用程序域并将插件程序集加载到应用程序域中。

在Windows上的Microsoft.NET上一切正常,但插件在Mac或Linux上运行的mono上不起作用。

尝试启动插件时,我收到如下异常:

无法强制转换类型为"System.Func"1[[API.Network.NodeType, API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 以键入 'System.Func'1[[API.Network.NodeType, API, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

这是因为每个插件都有自己的 API.dll 程序集副本,尽管程序集是相同的副本,但 mono 不会将类型视为相同。

如何让插件从主应用程序的目录中加载 API.dll?或者,或者,我怎样才能让单声道看到类型是相同的?

Mono 无法将 TypeA 转换为 TypeA

为了找到您问题的答案,我创建了一个简单的插件系统,并在Windows下的单声道3.2.3上成功对其进行了测试(不幸的是,我现在无法在Linux上进行测试,也许明天)。我的代码:

软件开发工具包

.dll
using System;
namespace SDK
{
    public interface IPlugin
    {
        void SomeMethod();
        SomeSDKType GetSDKType();
    }
}
using System;
using System.Collections.Generic;
namespace SDK
{
    [Serializable]
    public class StringEventArgs : EventArgs
    {
        public string Message { get; set; }
    }
    public class SomeSDKType : MarshalByRefObject
    {
        public event EventHandler<StringEventArgs> SDKEvent;
        public Action SDKDelegate;
        public void RiseSDKEvent(string message)
        {
            var handler = SDKEvent;
            if (handler != null) SDKEvent(this, new StringEventArgs { Message = message });
        }
        public Dictionary<int, string> GetDictionary()
        {
            var dict = new Dictionary<int, string> ();
            dict.Add(1, "One");
            dict.Add(2, "Two");
            return dict;
        }
    }
}

插件.dll

using System;
using SDK;
namespace Plugin
{
    public class Plugin : MarshalByRefObject, IPlugin
    {
        public Plugin()
        {
        }
        public void SomeMethod()
        {
            Console.WriteLine("SomeMethod");
        }
        public SomeSDKType GetSDKType()
        {
            var obj = new SomeSDKType();
            obj.SDKDelegate = () => Console.WriteLine("Delegate called from {0}", AppDomain.CurrentDomain.FriendlyName);
            return obj;
        }
    }
}

托管程序

using System;
using System.Reflection;
using System.IO;
using SDK;
namespace AppDomains
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var domain = AppDomain.CreateDomain("Plugin domain"); // Domain for plugins
            domain.Load(typeof(IPlugin).Assembly.FullName); // Load assembly containing plugin interface to domain 
            var currentPath = Directory.GetCurrentDirectory();
            var pluginPath = Path.Combine(currentPath, "Plugins");
            var pluginFiles = Directory.GetFiles(pluginPath, "*.dll");
            foreach (var pluginFile in pluginFiles) // Foreach dll in Plugins directory
            {
                var asm = Assembly.LoadFrom(pluginFile);
                foreach (var exportedType in asm.GetExportedTypes())
                {
                    if (!typeof(IPlugin).IsAssignableFrom(exportedType)) continue; // Check if exportedType implement IPlugin interface
                    domain.Load(asm.FullName); // If so load this dll into domain
                    var plugin = (IPlugin)domain.CreateInstanceAndUnwrap(asm.FullName, exportedType.FullName); // Create plugin instance
                    plugin.SomeMethod(); // Call plugins methods
                    var obj = plugin.GetSDKType();
                    obj.SDKDelegate();
                    var dict = obj.GetDictionary();
                    foreach (var pair in dict)
                    {
                        Console.WriteLine("{0} - {1}", pair.Key, pair.Value);
                    }
                    obj.SDKEvent += obj_SDKEvent;
                    obj.RiseSDKEvent(string.Format("Argument from domain {0}", AppDomain.CurrentDomain.FriendlyName));
                }
            }
            Console.ReadLine();
        }
        static void obj_SDKEvent(object sender, StringEventArgs e)
        {
            Console.WriteLine("Received event in {0}", AppDomain.CurrentDomain.FriendlyName);
            Console.WriteLine(e.Message);
        }
    }
}

应用配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <probing privatePath="Plugins"/>
    </assemblyBinding>
  </runtime>
</configuration>

对代码的一些解释。我创建了带有插件接口的 SDK dll。所有插件和主机应用程序都必须引用它。插件必须在没有 SDK dll 的情况下提供,因为主机应用已包含它。他们放入主机应用程序目录中的插件目录(即如果应用程序路径 = c:''MyApp,则插件位于 c:''MyApp''Plugins 中),因此为了提供 CLR(或单声道)查找插件组件的机会,我还创建了带有探测元素的 App.config 文件。

希望这有帮助。