473,327 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Create delegate to currently running method.

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?

Nov 16 '05 #1
3 3916
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?

Nov 16 '05 #2
> 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).

This is just stupid. It is the same problem like getting the current class
in a static method.
would be cool if the keyword class could be used to refer to the current
class and method or delegate to refer to the current method.

--
cody

Freeware Tools, Games and Humour
http://www.deutronium.de.vu || http://www.deutronium.tk
Nov 16 '05 #3
[cc'd in e-mail]
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).


This did exactly what I wanted. The code looks more or less like:

this.myself = (NextPanel)Delegate.CreateDelegate(this.next.GetTy pe(),
this,
System.Reflection.MethodBase.GetCurrentMethod().Na me);

A busy line for sure, but it's identical in 40-odd functions that needed to
set up this delegate.

Thank you.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
0
by: Ginno | last post by:
Hello all Within my reports form i have used delegates to allow a crystall reports to generate without holding up the user. The user selects a report from tree view, enters the appropriate...
2
by: Ruediger Klaehn | last post by:
I have written an arithmetic expression compiler. Now that it is finished I would like to give it a nice interface. Currently I have this interface: delegate double Function(double x); class...
0
by: Wavemaker | last post by:
One of the things I've struggled with from time to time is handling events raised on different threads. For example, an object could be listening to events from one or more objects running in...
15
by: Amit D.Shinde | last post by:
I am adding a new picturebox control at runtime on the form How can i create click event handler for this control Amit Shinde
4
by: Daylor | last post by:
hi. i have multi thread application in vb.net is there a way NET support, so i can mark the class , to be access only for 1 thread each time ? if there is , small sytax sample will help ...
3
by: Stewart | last post by:
Hey Group, Hoping someone can help me out. I have some code which starts up some asynchronous code using a delegate. The code is below. Basically my main code (not shown) calls...
11
by: ryan | last post by:
Hi, I've omitted a large chunk of the code for clarity but the loop below is how I'm calling a delegate function asynchronously. After I start the each call I'm incrementing a counter and then...
5
by: greg.merideth | last post by:
I have a class that I've provided an event for to be called when the processing in the class is complete (a callback). The class spins up a series of threads for the background operation and I'm...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.