Jump to content

  • Log In with Google      Sign In   
  • Create Account


C# DLL Plugin System, I want some input.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Simply Jarvis   Members   -  Reputation: 101

Like
0Likes
Like

Posted 11 October 2013 - 12:38 AM

So earlier today I saw the needs for a dll plugin system and coded one.  There is a API.dll and a Default.dll along with my Program.exe

 

Plugins/Default.dll

API.dll

Program.exe

 

My question is whats the best way to go about this ?  I want Program.exe to have the same types in API.dll but if I put those types in Program.exe and reference it from API.dll I would be able to explicity add the classes from 'Default.dll' via Assembly into Program.exe via static calls.  But then I would need to re-define the type in API.dll which wouldnt be to hard as I can just reference the ones already in Program.exe

So whats your thoughts and opinions on this one ?

 

 



Sponsor:

#2 PhillipHamlyn   Members   -  Reputation: 454

Like
0Likes
Like

Posted 11 October 2013 - 01:50 AM

Use Unity ? http://msdn.microsoft.com/en-us/library/dd203101.aspx



#3 Simply Jarvis   Members   -  Reputation: 101

Like
0Likes
Like

Posted 11 October 2013 - 02:01 AM

Thanks for the reply "PhillipHamlyn" but I can already load the classes and such within the plugins into my program so there is no need for external libraries, I simply want some input regarding how I should have the structure.

 

Example:

API.dll references Program.exe

Default.dll references API.dll

Program.exe loads all 'Plugins/*.dll' and pulls the classes from the plugins storing there types.

Program.exe then when in need of using the plugin types does "IMyPluginBaseType TheCreatedClass = (IMyPluginBaseType) Assembly.CreateInstance( Type.FullName );"

I store the data as such, Dictionary<String,Type>;  The string being a unique id generated when the type is parsed from the dll.

 

Currently I have Program.exe and Default.dll referencing API.dll as it's the easier meens of doing things.


Edited by Simply Jarvis, 11 October 2013 - 02:12 AM.


#4 PhillipHamlyn   Members   -  Reputation: 454

Like
0Likes
Like

Posted 11 October 2013 - 04:49 AM

Oh, I see.

 

If its a true plugin, the Program should not know about the Types inside the plugin - it should only share a set of Types (probably only Interfaces) made available for the plugin to consume, so for each plugin, as you say, there is only one Type you care about (which is the name of the Type implementing the Plugin interface). I'd probably parse the plugin assembly using reflection and pick up the class implementing the plugin Interface and instantiate it using CreateInstance as you suggest. Where is the need for Program to store the Types in the plugin ? I think I'm missing something ?

 

Philip



#5 Simply Jarvis   Members   -  Reputation: 101

Like
0Likes
Like

Posted 11 October 2013 - 05:08 AM

Well thats actually how I am doing things, as  for the types that get loaded into the program they get parsed by a static class in my Main file.  It grabs all the types from the plugins that implement classes or interfaces within API.dll So the types that implement ANY class in API.dll get added to a list for usage later in my program.

 

My main reason for posting this thread was to get input on how I should go about this and it would seem given your response that the way I've been planing to do it would indeed by recommended by you.



#6 RobTheBloke   Crossbones+   -  Reputation: 2324

Like
0Likes
Like

Posted 11 October 2013 - 05:18 AM

Sounds a lot like a thread I wrote ages ago? Would you be looking for this:

http://nccastaff.bournemouth.ac.uk/jmacey/RobTheBloke/www/CSPluginExample2.zip

 

I actually ported that to the latest .NET framework the other day. The code more or less still works (although I had to create new project files because for some unknown reason I couldn't get the project files to upgrade successfully - and couldn't be bothered to find out why). If you want to avoid the compiler warnings, modify the code like so:

 


    public static bool ExecuteVB(string source)
    {
        VBCodeProvider prov = new VBCodeProvider();
        return ExecuteScript(prov, source);
    }

    public static bool ExecuteCS(string source)
    {
        CSharpCodeProvider prov = new CSharpCodeProvider();
        return ExecuteScript(prov, source);
    }
 
    private static bool ExecuteScript(CodeDomProvider compiler, string code)
    {
        CompilerParameters cp = new CompilerParameters();
        cp.GenerateExecutable = false;
        cp.GenerateInMemory = true;
 
        //
        // add any default libs you want to expose to the scripting API
        //
        cp.ReferencedAssemblies.Add("system.dll");
        cp.ReferencedAssemblies.Add("system.data.dll");
        cp.ReferencedAssemblies.Add("system.drawing.dll");
        cp.ReferencedAssemblies.Add("system.windows.forms.dll");
 
        //
        // add reference to the core dll(s) of the application. The scripts
        // will then be able to access the core functionality of the APP.
        //
        cp.ReferencedAssemblies.Add("CoreAPI.dll");
 
        CompilerResults cr;
        cr = compiler.CompileAssemblyFromSource(cp, code);
 
        // check for any errors in the script
        if (cr.Errors.HasErrors)
        {
            StringBuilder sbErr;
            sbErr = new StringBuilder();
            foreach (CompilerError err in cr.Errors)
            {
                sbErr.AppendFormat("{0} at line {1} column {2} ",
                     err.ErrorText,
                     err.Line,
                     err.Column);
                sbErr.Append("\n");
            }
            MessageBox.Show(sbErr.ToString(), "Script - Error");
            return false;
        }
 
        // get the assembly code generated
        Assembly a = cr.CompiledAssembly;
        try
        {
            return InvokeAssembly(a);
        }
        catch (Exception ex)
        {
            // this is called if an error gets generated when running the script
            MessageBox.Show(ex.Message);
        }
        return false;
    }

The basic premise is that you define the base types in the API.dll, load the plugin assembly, and then use reflection to hunt for any classes in the plugin that support any of your supported interfaces. It's probably easier to do this with interfaces rather than base classes!

 

\edit

 

Found the original thread: http://www.gamedev.net/topic/473195-saveload-as-plugin/


Edited by RobTheBloke, 11 October 2013 - 05:21 AM.


#7 Simply Jarvis   Members   -  Reputation: 101

Like
0Likes
Like

Posted 11 October 2013 - 05:41 AM

I already have what I was looking for...  But anyway that is very intresting code non the less, though I don't currently have use for it.



#8 Xai   Crossbones+   -  Reputation: 1338

Like
0Likes
Like

Posted 11 October 2013 - 09:57 AM

For everyone interested, here is the article (from GameDev) that taught me how to build plugin systems over 10 years ago:

 

http://www.gamedev.net/page/resources/_/technical/multiplayer-and-network-programming/why-pluggable-factories-rock-my-multiplayer-world-r841

 

it is a very helpful article and I have used its core ideas a dozen times since (the implementation details vary by language - but the core concept of derived types "registering" themselves is the key).






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS