0

I'm trying to create an instance of my custom class, ServerPlugin, from a DLL file, however, I'm getting the exception:

TypeLoadException: Could not load type 'ServerPlugin'.

This is my main code, that will create an instance of ServerPlugin:

public class Main : MonoBehaviour
{    
    void Start()
    {
        string path = System.IO.Path.Combine(Application.streamingAssetsPath, "ProjectIGTestMod.dll");

        // Domain
        AppDomain domain = AppDomain.CreateDomain("Plugin");
        Type type = typeof(ServerPlugin);

        // Create instance of plugin
        ServerPlugin plugin = null;
        try 
        {
            plugin = domain.CreateInstanceFromAndUnwrap(path, type.FullName) as ServerPlugin;
        }
        catch (Exception e) 
        {
            Debug.LogException(e);
        }

        // If loaded plugin
        if (plugin != null)
        {
            // Load and enable
            //plugin.onLoad();
            plugin.onEnable();
        }
    }
}

And here is the class I'm trying to create an instance of:

namespace ProjectIGTestMod
{
    public class Main : ServerPlugin
    {
        public void onLoad()
        {
            throw new System.NotImplementedException();
        }

        public void onEnable()
        {
            Debug.Log("Test Mod enabled.");
        }

        public void onDisable()
        {
            throw new System.NotImplementedException();
        }
    }
}

I'm not sure what exactly I'm doing wrong. I tried to follow this code.

Community
  • 1
  • 1
Vapid
  • 701
  • 7
  • 27
  • What binary ServerPlugin is defined in and what binary `public class Main : MonoBehaviour` from above is defined in? If you can do `typeof(ServerPlugin)` in your code this means that the dll with `ServerPlugin` is already loaded. I suspect that you have two `ServerPlugin` classes defined with the same name and try to load instance of one into variable of type of the other. That won't work. – Andrew Savinykh Aug 24 '14 at 21:15
  • And if it's the same `ServerPlugin` then instead of `plugin = domain.CreateInstanceFromAndUnwrap(path, type.FullName) as ServerPlugin;` just do `plugin = new ServerPlugin();` – Andrew Savinykh Aug 24 '14 at 21:17
  • The ServerPlugin interface is in the main project. The instance itself uses the SAME interface, but is exported into a DLL file. I'm trying to load DLL files as plugins, so I can't just create a new instance of ServerPlugin. – Vapid Aug 25 '14 at 14:03
  • The DLL with the INTERFACE ServerPlugin is already loaded. But I'm trying to load the ServerPlugin instance itself, which is located in a DLL file. The ServerPlugin interface is the same. – Vapid Aug 25 '14 at 14:05
  • You can't instantiate a c# interface. This is because interfaces do not contain any implementation. To create an instance you must have a class. – Andrew Savinykh Aug 25 '14 at 18:51
  • Perhaps you could post definition of ServerPlugin from the main project and also explain (with code) how you "export" it into a DLL? – Andrew Savinykh Aug 25 '14 at 20:29
  • I fixed my issue by passing the type name as `Namespace.TypeName` instead of just `TypeName` – Martin Lottering Aug 13 '15 at 09:44

0 Answers0