I have a winform application(WindowsFormsApplication2) that loads a dll using appdomain. In the winform, I have suscribed to 'TestEvent' implemented in ClassLibrary.dll. If the TestEvent returns an argument,how can I capture the returned argument in the event handler HandleEvent implemented in winform?
This is the winform application codes.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Diagnostics;
using ClassLibrary1;
using static System.Net.Mime.MediaTypeNames;
namespace WindowsFormsApplication2
{
  
    [Serializable]
    public partial class Form1 : Form
    {
        void HandleEvent(object sender, EventArgs e)
        {
            Debug.WriteLine("HandleEvent called");
        }
        string DLL = @"..\ConsoleApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
        public Form1()
        {
            InitializeComponent();
            Loader.Call( DLL, "ClassLibrary1.Class1", "RaiseEvent", HandleEvent, DateTime.Now.ToShortDateString());
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Application.Restart();
            Application.Run(new Form1());
            this.Close();
        }
     
    }
    public class Loader : MarshalByRefObject
    {
        AppDomain ad = AppDomain.CreateDomain("Test");
        object CallInternal(string dll, string typename, string method, EventHandler handler, object[] parameters)
        {
            Assembly a = Assembly.LoadFile(dll);
            object o = a.CreateInstance(typename);
            Type t = o.GetType();
            // Subscribe to the event
            EventInfo eventInfo = t.GetEvent("TestEvent");
            eventInfo.AddEventHandler(o, handler);
            MethodInfo m = t.GetMethod(method);
            return m.Invoke(o, parameters);
        }
        public static object Call( string dll, string typename, string method, EventHandler handler, params object[] parameters)
        {
            Loader ld = (Loader)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
            object result = ld.CallInternal(dll, typename, method, handler, parameters);
            AppDomain.Unload(ad);
            return result;
        }
    }
}
This is the ClassLibrary1.dll codes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassLibrary1
{
    [Serializable]
    public class Class1
    {
        public event EventHandler TestEvent;
        public int RaiseEvent(string msg)
        {
            try
            {
                TestEvent(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                Console.WriteLine("the exception is: " + ex.ToString());
                if (ex.InnerException != null)
                {
                    Console.WriteLine("the inner exception is: " + ex.InnerException.Message.ToString());
                }
            }
            return 2;
        }
    }
}
 
     
    