K
Klaudiusz Bryja
Hi,
This is for NetCF 2.0.
I need to create event handling code which using reflection. I have
some parameters in XML which describe how event should be handled. I
have code to create delegate:
public class DelegateEx
{
private object target;
private MethodInfo method;
private XmlNode node;
// XmlNode describe how to handle event (dll path, class, method and
method parameters).
private DelegateEx(MethodInfo method, object target, XmlNode
node)
{
this.target = target;
this.method = method;
this.node = node;
}
// This is called when event was fired.
// XMLFormsEventArgs inherits from EventsArgs and add XmlNode
property.
private void Invoke(object sender, EventArgs e)
{
method.Invoke(target, new object[] { sender, (new
XMLFormsEventArgs(this.node, e) as EventArgs) });
}
public static EventHandler CreateDelegate(MethodInfo method,
object target, XmlNode node)
{
return new EventHandler((new DelegateEx(method, target,
node)).Invoke);
}
}
Using this I try to subscribe to event (I have static method
HandleEvents which should be called when event is fired):
EventInfo ei = form.GetType().GetEvent("Click",
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Instance | BindingFlags.Static);
if (ei != null)
{
MethodInfo miHandler = typeof(XMLForms).GetMethod("HandleEvents",
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Instance | BindingFlags.Static);
MethodInfo addHandler = ei.GetAddMethod();
// formNode - this is xmlNode which describe event
Object[] addHandlerArgs = { DelegateEx.CreateDelegate(miHandler,
typeof(XMLForms), formNode) };
addHandler.Invoke(form, addHandlerArgs);
}
Everything work when I want to handle standard event which is type of
EventHandler. But I get error "ArgumentException" without any message
when I try to handle e.g. KeyDown event (event is type of
KeyEventHandler).
StackTrace for this error:
at System.Reflection.RuntimeMethodInfo.InternalInvoke()
at System.Reflection.RuntimeMethodInfo.InternalInvoke()
at System.Reflection.RuntimeMethodInfo.Invoke()
at System.Reflection.MethodBase.Invoke()
at bcs.XMLForms.BuildXMLForm()
at TestApp.Form1.button2_Click()
at System.Windows.Forms.Control.OnClick()
at System.Windows.Forms.Button.OnClick()
at System.Windows.Forms.ButtonBase.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at TestApp.Program.Main()
I think that I should cast EventHandler type to KeyEventHandler type
but I get error:
Cannot convert type 'System.EventHandler' to
'System.Windows.Forms.KeyEventHandler'.
How to change my code to work with all type of EventHandler?
Best regards,
Klaudiusz
This is for NetCF 2.0.
I need to create event handling code which using reflection. I have
some parameters in XML which describe how event should be handled. I
have code to create delegate:
public class DelegateEx
{
private object target;
private MethodInfo method;
private XmlNode node;
// XmlNode describe how to handle event (dll path, class, method and
method parameters).
private DelegateEx(MethodInfo method, object target, XmlNode
node)
{
this.target = target;
this.method = method;
this.node = node;
}
// This is called when event was fired.
// XMLFormsEventArgs inherits from EventsArgs and add XmlNode
property.
private void Invoke(object sender, EventArgs e)
{
method.Invoke(target, new object[] { sender, (new
XMLFormsEventArgs(this.node, e) as EventArgs) });
}
public static EventHandler CreateDelegate(MethodInfo method,
object target, XmlNode node)
{
return new EventHandler((new DelegateEx(method, target,
node)).Invoke);
}
}
Using this I try to subscribe to event (I have static method
HandleEvents which should be called when event is fired):
EventInfo ei = form.GetType().GetEvent("Click",
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Instance | BindingFlags.Static);
if (ei != null)
{
MethodInfo miHandler = typeof(XMLForms).GetMethod("HandleEvents",
BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic
| BindingFlags.Instance | BindingFlags.Static);
MethodInfo addHandler = ei.GetAddMethod();
// formNode - this is xmlNode which describe event
Object[] addHandlerArgs = { DelegateEx.CreateDelegate(miHandler,
typeof(XMLForms), formNode) };
addHandler.Invoke(form, addHandlerArgs);
}
Everything work when I want to handle standard event which is type of
EventHandler. But I get error "ArgumentException" without any message
when I try to handle e.g. KeyDown event (event is type of
KeyEventHandler).
StackTrace for this error:
at System.Reflection.RuntimeMethodInfo.InternalInvoke()
at System.Reflection.RuntimeMethodInfo.InternalInvoke()
at System.Reflection.RuntimeMethodInfo.Invoke()
at System.Reflection.MethodBase.Invoke()
at bcs.XMLForms.BuildXMLForm()
at TestApp.Form1.button2_Click()
at System.Windows.Forms.Control.OnClick()
at System.Windows.Forms.Button.OnClick()
at System.Windows.Forms.ButtonBase.WnProc()
at System.Windows.Forms.Control._InternalWnProc()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at TestApp.Program.Main()
I think that I should cast EventHandler type to KeyEventHandler type
but I get error:
Cannot convert type 'System.EventHandler' to
'System.Windows.Forms.KeyEventHandler'.
How to change my code to work with all type of EventHandler?
Best regards,
Klaudiusz