2

I'm having difficulties trying to run a .dll in a new AppDomain. My object is always of type System.MarshalByRefObject, so I cannot get the methods from the plugin.

What I have right now, is a plugin, that implements an interface and extending a MarshalByRefObject, looking like this:

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

Then I have my plugin implemented like this:

[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
    }
}

So I built the plugin, got the dll, placed it in a folder and now in my project I'm trying to load it like this:

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
}

But it crashes on Execute.Invoke(...), because it doesn't know the method Execute, since the object is of wrong type.

Error message is:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tomáš Ptáček
  • 53
  • 1
  • 1
  • 6

0 Answers0