473,654 Members | 3,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Syste m.Reflection.Me thodBase.GetCur rentMethod()... .);

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 3931
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 GetCurrentMetho d 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.co m

"Clinton Pierce" <cp*****@payrol l1.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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(Syste m.Reflection.Me thodBase.GetCur rentMethod()... .);

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 GetCurrentMetho d 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)Dele gate.CreateDele gate(this.next. GetType(),
this,
System.Reflecti on.MethodBase.G etCurrentMethod ().Name);

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
2186
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 reflection. The code I supplied is based on the SqlDataAdapter with reflection. The error occurs when trying to create the delegate that will be passed in to EventInfo.AddEventHandler. I get the following error: An unhandled exception of type...
0
1060
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 data and then hits the "Display" button. At this point the delegate is created and fired via BeginInvoke. The code run within the delegate call includes calls to the database
2
1733
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 ArithmeticExpression { public static Function Compile(string text); ... }
0
2495
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 different threads besides itself. There is a concern for thread safety. There are various ways of dealing with this. One way is to make the class thread safe by putting in locks. But another way is to stream the events from the various threads into a...
15
26503
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
1547
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 //what i need to add , so only 1 thread per time can access this class in MultiThread app.
3
5841
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 ServerThreadStart.StartServer to start the server running asynchronously. This works fine. Shouldn't be any problems here. My question is how can I get my code to kill this code running asynchronously? There is a dlgtServer.Remove(Delegate, Delegate)
11
6954
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 making the main thread sleep until the counter gets back to zero. The call back function for each call decrements the counter. Is there a better way to make the thread wait until all calls are complete besides using the counter? I've seen some things...
5
1766
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 firing the callback delgate from within one of the new threads. So far, all is well but I'm curious as to what, threading wise, is going on there. I'm instantiating the class in the app thread (say 1) and providing a method to call when...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.