Cannot convert from method group to System.EventHandler. Changing handler signature shows error when dealing with appdomain
Hi,
I have the following two programs (1) Windows forms application (2) ClassLibrary1.dll
In the winform, the dll is loaded using appdomain. The winform subscribes to the action('TestAction') in the dll. I am trying to access the value returned by the dll action and use it in the event handler('HandleAction'). To do this I created a child class that inherits EventArgs. I changed the input arguments of the event handler(HandleAction) accordingly. But I am seeing a compiler error at Loader.Call( "RaiseAct", HandleAction, DateTime.Now.ToShortDateString()); . The error is:Cannot convert from method group to System.EventHandler
Any help will be very much appreciated. Thank you.
This is winform code:
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;
namespace WindowsFormsApplication2
{
[Serializable]
public class IntEventArgs : EventArgs
{
public int data;
}
[Serializable]
public partial class Form1 : Form
{
void HandleEvent(object sender, EventArgs e)
{
Debug.WriteLine("HandleEvent called");
}
void HandleAction(object sender, IntEventArgs e)
{
Debug.WriteLine("HandleAction called");
Debug.WriteLine("the value of index is " + e.data);
}
public Form1()
{
InitializeComponent();
Loader.Call( "RaiseEvent", HandleEvent, DateTime.Now.ToShortDateString());
Loader.Call( "RaiseAct", HandleAction, DateTime.Now.ToShortDateString()); // Cannot convert from method group to System.EventHandler
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
Application.Run(new Form1());
this.Close();
}
}
public class Loader : MarshalByRefObject
{
static string dll = @"..\ConsoleApplication1\ClassLibrary1\bin\Debug\ClassLibrary1.dll";
static AppDomain ad = AppDomain.CreateDomain("Test");
static Assembly a = Assembly.LoadFile(dll);
static object o = a.CreateInstance("ClassLibrary1.Class1");
static Type t = o.GetType();
object CallInternal1( string method, EventHandler handler, object[] parameters)
{
// Subscribe to the event
EventInfo eventInfo1 = t.GetEvent("TestEvent");
eventInfo1.AddEventHandler(o, handler);
MethodInfo m = t.GetMethod(method);
return m.Invoke(o, parameters);
}
object CallInternal2( string method, EventHandler handler, object[] parameters)
{
// Subscribe to the event
EventInfo eventInfo2 = t.GetEvent("TestAction");
eventInfo2.AddEventHandler(o, new Action<int>((index) => handler(null, new IntEventArgs { data = index })));
MethodInfo m = t.GetMethod(method);
return m.Invoke(o, parameters);
}
public static object Call( string method, EventHandler handler, params object[] parameters)
{
Loader ld = (Loader)ad.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(Loader).FullName);
object result = 0;
switch (method)
{
case "RaiseEvent":
{
result = ld.CallInternal1( method, handler, parameters);
break;
}
case "RaiseAct":
{
result = ld.CallInternal2( method, handler, parameters);
break;
}
}
return result;
}
}
}
This is ClassLibrary1.dll code:
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;
}
public event Action<int> TestAction = Func;
public int RaiseAct(string msg)
{
TestAction(3);
return 5;
}
public static void Func(int a)
{
int g = 2;
}
}
}