Search code examples
c#.netadd-on

Design add-on base Application


I want to know that how design application , actually applications like Firefox or Chrome that you can download add-on for them and use ??!! How do it in .Net ???


Solution

  • How to allow others to make Add-Ons for your app?

    1>You create a DLL which has an interface.This interface defines a set of methods,properties,events you want others to define and implement.

    the plugin developer need to define this interface.This DLL is required by your app and the plugin developer..

    2>The plugin developer would use that shared DLL and implement the interface by defining the methods or properties in it.

    3>Your app would now load that plugin and cast it to the shared DLL's interface and then call the desired methods,properties i.e anything that was defined in the interface..

    How would your App fetch plugins?

    You create a folder where you would search for the plugins.This is the folder where others plugins would be installed or placed.


    Example

    This is your shared dll

    //this is the shared plugin
    namespace Shared 
    {
        interface IWrite 
        {
            void write();
        }
    }
    

    The plugin devloper

    //this is how plugin developer would implement the interface
    using Shared;//<-----the shared dll is included
    namespace PlugInApp 
    {
        public class plugInClass : IWrite //its interface implemented
        {
            public  void write() 
            {
                Console.Write("High from plugInClass");
            }
        }
    }
    

    this is your program

    using Shared;//the shared plugin is required for the cast
    class Program
        {
            static void Main(string[] args)
            {
                //this is how you search in the folder
                foreach (string s in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*PlugIn.dll"))//getting plugins in base directory ending with PlugIn.dll
                {
                    Assembly aWrite = Assembly.LoadFrom(s);
                    //this is how you cast the plugin with the shared dll's interface
                    Type tWrite = aWrite.GetType("PlugInApp.plugInClass");
                    IWrite click = (IWrite)Activator.CreateInstance(tWrite);//you create the object
                    click.write();//you call the method
                }
            } 
        }