Plugin architecture and event handlers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anton Zinchenko
    New Member
    • Sep 2010
    • 16

    Plugin architecture and event handlers

    I'm trying to write some kind of plugin framework... It looks something like this:
    Host Application (Windows Form Application) loads plugins into separate AppDomains (each plugin into own AppDomain).
    How to set a method from plugin as eventhandler for MenuItem Click event (MenuStrip is on the main form of host application) and prevent loading plugin assembly into the main AppDomain?
  • Anton Zinchenko
    New Member
    • Sep 2010
    • 16

    #2
    I'm still has no luck with this question...

    Host application:
    Code:
        public partial class Form1 : Form
        {
            private AssemblyLoader _aLoader = null;
    
            public Form1()
            {
                InitializeComponent();
    
                AppDomain _domain = AppDomain.CreateDomain("Remote Load");
                _aLoader = (AssemblyLoader)_domain.CreateInstanceAndUnwrap(
                                         "Loader", "Loader.AssemblyLoader");
    
                _aLoader.Load();
    
                EventHandler eh = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), _aLoader.Instance, _aLoader.Method);
    
                button1.Click += eh;
            }
        }
    AssemblyLoader:
    Code:
        [Serializable]
        public class AssemblyLoader : MarshalByRefObject
        {
            object _instance = null;
            MethodInfo _method = null;
    
            public void Load()
            {
                Assembly _assembly = Assembly.LoadFile(Environment.CurrentDirectory + @"\Plugin.dll");
                Type _type = _assembly.GetType("Plugin.Test");
                _instance = Activator.CreateInstance(_type);
                _method = _instance.GetType().GetMethod("TestEventHandler");
            }
    
            public MethodInfo Method
            {
                get { return _method; }
            }
    
            public object Instance
            {
                get { return _instance; }
            }
        }
    Plugin:
    Code:
        [Serializable]
        public class Test
        {
            public void TestEventHandler(object sender, System.EventArgs e)
            {
                MessageBox.Show("Pingback from plugin");
            }      
        }
    In this case button click causes plugin.dll to load into the main appdomain... =(
    Any suggestions how to prevent this?

    Comment

    Working...