Clinton,
You will want to get the current MethodInfo instance for the currently
executing method. You will have to then pass that MethodInfo instance to
the static CreateDelegate method on the Delegate class.
The CreateDelegate method has an overload that takes a MethodInfo
instance. If you can guarantee that your code will never be called by a
constructor, then you could cast the value returned from GetCurrentMethod to
MethodInfo and pass that to CreateDelegate, along with the type of the
delegate. I think that this is a safe guarantee, since you can't attach a
delegate to a constructor.
However, I would avoid it, only because you don't know what the runtime
might return in the future (something else derived form MethodBase, but not
MethodInfo). For that, I would use the overload that takes the instance of
the object (you pass this) and the name of the method (you can get the
MethodBase instance and then pass the Name property).
Also, you might want to name your delegates with a pattern that is
dependent on the type that they are declared in. This will allow you to
make the code more generic (and possibly refactor it to one place).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Clinton Pierce" <cp*****@payroll1.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I can create a delegate like this, and everything works fine:
class Foo
{
private delegate void NextPanel();
private NextPanel myself;
// And later in a method
private void EffStart()
{
this.myself = new NextPanel(this.EffStart);
}
}
This program has lots of these self-referential delegates. What I'd like
to do is have a statement that simply says:
this.myself = new NextPanel(some reference back to the current running
method);
So I can avoid having to put the method name in the assignment (a source
of human errors, for sure). But I can't quite find the syntax. I've tried
most of:
this.myself = new
NextPanel(System.Reflection.MethodBase.GetCurrentM ethod()....);
But can't find anything in there to give me the right type for the
delegate to make the compiler happy. Any recommendations?