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(Meth odInfo method, object target, XmlNode
node)
{
this.target = target;
this.method = method;
this.node = node;
}
// This is called when event was fired.
// XMLFormsEventAr gs inherits from EventsArgs and add XmlNode
property.
private void Invoke(object sender, EventArgs e)
{
method.Invoke(t arget, new object[] { sender, (new
XMLFormsEventAr gs(this.node, e) as EventArgs) });
}
public static EventHandler CreateDelegate( MethodInfo method,
object target, XmlNode node)
{
return new EventHandler((n ew DelegateEx(meth od, 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.Ig noreCase | BindingFlags.Pu blic | BindingFlags.No nPublic
| BindingFlags.In stance | BindingFlags.St atic);
if (ei != null)
{
MethodInfo miHandler = typeof(XMLForms ).GetMethod("Ha ndleEvents",
BindingFlags.Ig noreCase | BindingFlags.Pu blic | BindingFlags.No nPublic
| BindingFlags.In stance | BindingFlags.St atic);
MethodInfo addHandler = ei.GetAddMethod ();
// formNode - this is xmlNode which describe event
Object[] addHandlerArgs = { DelegateEx.Crea teDelegate(miHa ndler,
typeof(XMLForms ), formNode) };
addHandler.Invo ke(form, addHandlerArgs) ;
}
Everything work when I want to handle standard event which is type of
EventHandler. But I get error "ArgumentExcept ion" without any message
when I try to handle e.g. KeyDown event (event is type of
KeyEventHandler ).
StackTrace for this error:
at System.Reflecti on.RuntimeMetho dInfo.InternalI nvoke()
at System.Reflecti on.RuntimeMetho dInfo.InternalI nvoke()
at System.Reflecti on.RuntimeMetho dInfo.Invoke()
at System.Reflecti on.MethodBase.I nvoke()
at bcs.XMLForms.Bu ildXMLForm()
at TestApp.Form1.b utton2_Click()
at System.Windows. Forms.Control.O nClick()
at System.Windows. Forms.Button.On Click()
at System.Windows. Forms.ButtonBas e.WnProc()
at System.Windows. Forms.Control._ InternalWnProc( )
at Microsoft.AGL.F orms.EVL.EnterM ainLoop()
at System.Windows. Forms.Applicati on.Run()
at TestApp.Program .Main()
I think that I should cast EventHandler type to KeyEventHandler type
but I get error:
Cannot convert type 'System.EventHa ndler' to
'System.Windows .Forms.KeyEvent Handler'.
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(Meth odInfo method, object target, XmlNode
node)
{
this.target = target;
this.method = method;
this.node = node;
}
// This is called when event was fired.
// XMLFormsEventAr gs inherits from EventsArgs and add XmlNode
property.
private void Invoke(object sender, EventArgs e)
{
method.Invoke(t arget, new object[] { sender, (new
XMLFormsEventAr gs(this.node, e) as EventArgs) });
}
public static EventHandler CreateDelegate( MethodInfo method,
object target, XmlNode node)
{
return new EventHandler((n ew DelegateEx(meth od, 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.Ig noreCase | BindingFlags.Pu blic | BindingFlags.No nPublic
| BindingFlags.In stance | BindingFlags.St atic);
if (ei != null)
{
MethodInfo miHandler = typeof(XMLForms ).GetMethod("Ha ndleEvents",
BindingFlags.Ig noreCase | BindingFlags.Pu blic | BindingFlags.No nPublic
| BindingFlags.In stance | BindingFlags.St atic);
MethodInfo addHandler = ei.GetAddMethod ();
// formNode - this is xmlNode which describe event
Object[] addHandlerArgs = { DelegateEx.Crea teDelegate(miHa ndler,
typeof(XMLForms ), formNode) };
addHandler.Invo ke(form, addHandlerArgs) ;
}
Everything work when I want to handle standard event which is type of
EventHandler. But I get error "ArgumentExcept ion" without any message
when I try to handle e.g. KeyDown event (event is type of
KeyEventHandler ).
StackTrace for this error:
at System.Reflecti on.RuntimeMetho dInfo.InternalI nvoke()
at System.Reflecti on.RuntimeMetho dInfo.InternalI nvoke()
at System.Reflecti on.RuntimeMetho dInfo.Invoke()
at System.Reflecti on.MethodBase.I nvoke()
at bcs.XMLForms.Bu ildXMLForm()
at TestApp.Form1.b utton2_Click()
at System.Windows. Forms.Control.O nClick()
at System.Windows. Forms.Button.On Click()
at System.Windows. Forms.ButtonBas e.WnProc()
at System.Windows. Forms.Control._ InternalWnProc( )
at Microsoft.AGL.F orms.EVL.EnterM ainLoop()
at System.Windows. Forms.Applicati on.Run()
at TestApp.Program .Main()
I think that I should cast EventHandler type to KeyEventHandler type
but I get error:
Cannot convert type 'System.EventHa ndler' to
'System.Windows .Forms.KeyEvent Handler'.
How to change my code to work with all type of EventHandler?
Best regards,
Klaudiusz
Comment