0

我有两个应用程序。它们共享共同的功能和插件。为了兼容,两者都使用相同的命名空间结构。这些应用程序将插件加载为 cs 并将它们编译为 dll。

与 cs 相同的插件适用于两个应用程序 - 从源代码加载并编译为 dll。下次运行时,应用程序仅加载 dll 文件。

我想只允许在没有源代码的情况下将插件作为 dll 分发。问题是如果一个 dll 是从第一个应用程序编译的,它不能从第二个应用程序加载。

在下图中,我分别从每个应用程序中编译了“自适应 MACD”。

比较清单 当我尝试从第二个应用程序加载第一个应用程序时,我收到以下错误消息:

"无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。 "

这是我的代码:

public void LoadDllIndicator(string dllPath, out string errorMessages)
{
    errorMessages = string.Empty;
    Assembly assembly = Assembly.LoadFrom(dllPath);
    try
    {
        Type[] types = assembly.GetTypes();

        foreach (Type type in types)
        {
            if (!typeof (IIndicator).IsAssignableFrom(type))
                continue;

            var newIndicator = Activator.CreateInstance(type) as Indicator;

            if (newIndicator == null)
            {
                errorMessages = "Cannot load: " + dllPath;
                return;
            }

            newIndicator.Initialize(SlotTypes.NotDefined);
            newIndicator.CustomIndicator = true;
            newIndicator.LoaddedFromDll = true;
            IntegrateIndicator(dllPath, out errorMessages, newIndicator);
        }
    }
    catch (Exception exception)
    {
        errorMessages = "ERROR: Loading '" + Path.GetFileName(dllPath) + "': " + exception.Message;
        if (exception.InnerException != null && !string.IsNullOrEmpty(exception.InnerException.Message))
            errorMessages += " " + exception.InnerException.Message;
    }
}

如何使从其中一个应用程序中编译的 dll 在另一个应用程序中运行成为可能?

4

0 回答 0