2

我在尝试在新的 AppDomain 中运行 .dll 时遇到了困难。我的对象总是 type System.MarshalByRefObject,所以我无法从插件中获取方法。

我现在拥有的是一个插件,它实现了一个接口并扩展了一个MarshalByRefObject,如下所示:

public interface IPlugin
{
    string Name { get; }
    string Description { get; }
    string Author { get; }
    void Execute();
}

然后我的插件是这样实现的:

[Serializable]
public class IPPlugin : MarshalByRefObject, IPlugin
{
    public string Author
    {
        get
        {
            return "John John";
        }
    }

    public string Description
    {
        get
        {
            return "description";
        }
    }

    public string Name
    {
        get
        {
            return "name";
        }
    }

    public void Execute()
    {
    //do stuff here
    }
}

所以我构建了插件,得到了 dll,把它放在一个文件夹中,现在在我的项目中,我试图像这样加载它:

AppDomain domain = AppDomain.CreateDomain("PluginDomain");
Object obj = domain.CreateInstanceFromAndUnwrap(path + "\\" + plugins[option].getAssemblyName(), plugins[option].getTypeName());

Console.WriteLine(obj.GetType());

if (RemotingServices.IsTransparentProxy(obj))
{
    Type type = obj.GetType();
    MethodInfo Execute = type.GetMethod("Execute");
    Execute.Invoke(obj, null); //crashes here
}

但它崩溃了Execute.Invoke(...),因为它不知道方法Execute,因为对象的类型错误。

错误信息是:

未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例。

4

0 回答 0