Dynamically registering Delegate late using reflection C# VS2008

M

mark e. barron

using System.Reflection

namespace WindowsFormsApplication2
{

public partial class GameForm : Form
{
...

private void GameForm_Load(object sender, EventArgs e)
{
...
TextBox tx = new TextBox();
tx.Name = "txt" + y.ToString() + x.ToString();
...
string sMethodName= tx.Name + "_MouseClick";
Type t = Type.GetType("WindowsFormsApplication2.GameForm");
MethodInfo mi = t.GetMethod(sMethodName);
//tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())

.......

public void txt00_MouseClick(object sender, MouseEventArgs e)
{

--------------------------------

I want to dynamically register an event handler with an event for
a newly created TextBox using C# and reflection. I have used VS2008
to create a TableLayoutPanel and now want to populate it with
TextBoxes.

mi.MethodHandle.GetFunctionPointer() returns System.IntPtr
Needed here is System.EventHandler, I believe.
I cannot cast
(System.EventHandler) System.IntPtr.

The Code compiles and runs as intended as long as I comment out

tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())

which is the whole purpose of this effort.

Please advise. I am stuck.
 
A

Anthony Jones

mark e. barron said:
using System.Reflection

namespace WindowsFormsApplication2
{

public partial class GameForm : Form
{
...
private void GameForm_Load(object sender, EventArgs e)
{ ...
TextBox tx = new TextBox();
tx.Name = "txt" + y.ToString() + x.ToString();
...
string sMethodName= tx.Name + "_MouseClick";
Type t = Type.GetType("WindowsFormsApplication2.GameForm");
MethodInfo mi = t.GetMethod(sMethodName);
//tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())
.......
public void txt00_MouseClick(object sender, MouseEventArgs e)
{

--------------------------------

I want to dynamically register an event handler with an event for
a newly created TextBox using C# and reflection. I have used VS2008
to create a TableLayoutPanel and now want to populate it with
TextBoxes.

mi.MethodHandle.GetFunctionPointer() returns System.IntPtr
Needed here is System.EventHandler, I believe.
I cannot cast
(System.EventHandler) System.IntPtr.

The Code compiles and runs as intended as long as I comment out

tx.MouseClick += new System.EventHandler(
mi.MethodHandle.GetFunctionPointer())

which is the whole purpose of this effort.

Please advise. I am stuck.

The Method handle and GetFunctionPointer method are for interop situations
not for internal .NET only circimstances.

You want something _like_ :-

tx.MouseClick +=
(MouseClickHandler)Delegate.CreateDelegate(typeof(MouseClickHandler), mi);

I emphasise _like_ because the above only works if the method that mi is
holding is a static method. If not you will need an instance of
WindowsFormsApplication2.GameForm to pass to CreateDelegate, assuming that
is the current form then:-

tx.MouseClick +=
(MouseClickHandler)Delegate.CreateDelegate(typeof(MouseClickHandler), this,
mi);
 
M

mark e. barron

Anthony said:
The Method handle and GetFunctionPointer method are for interop
situations not for internal .NET only circimstances.

You want something _like_ :-

tx.MouseClick +=
(MouseClickHandler)Delegate.CreateDelegate(typeof(MouseClickHandler), mi);

I emphasise _like_ because the above only works if the method that mi is
holding is a static method. If not you will need an instance of
WindowsFormsApplication2.GameForm to pass to CreateDelegate, assuming
that is the current form then:-

tx.MouseClick +=
(MouseClickHandler)Delegate.CreateDelegate(typeof(MouseClickHandler),
this, mi);
Thanks so much Anthony for the CreateDelegate heads up. I got the
following actually working!

string sMethodName= tx.Name + "_MouseClick";
Type t = Type.GetType("WindowsFormsApplication2.GameForm");
MethodInfo mi = t.GetMethod(sMethodName);
MouseEventHandler eh =
(MouseEventHandler)System.Delegate.CreateDelegate(typeof(MouseEventHandler),this,
mi,false);
tx.MouseClick += new MouseEventHandler (eh);
 
M

mark e. barron

mark said:
Thanks so much Anthony for the CreateDelegate heads up. I got the
following actually working!

string sMethodName= tx.Name + "_MouseClick";
Type t = Type.GetType("WindowsFormsApplication2.GameForm");
MethodInfo mi = t.GetMethod(sMethodName);
MouseEventHandler eh =
(MouseEventHandler)System.Delegate.CreateDelegate(typeof(MouseEventHandler),this,
mi,false);
tx.MouseClick += new MouseEventHandler (eh);

Sorry, the Copy Constructor invocation is unnecessary. The final - more
economical - line would be:
tx.MouseClick += eh;
I am not writing this for you, but rather for others who may
view. Thanks again Anthony.
 
Top