How to retrieve a method's name as a string?

P

proxyuser

I want to do something like this

string s = x.method().AsString()

meaning, I'd like to programmatically retrieve the method name (not the name
of the method currently running, but the name of some other known method.)

The reason I want to do this is I want to display as output to the user the
name of the method being called. I don't want to hard code this value
because if the method name ever changes, I don't want to count on the dev
remembering to change the output string. It would be acceptable to code it
in such a way that it caused a (1) runtime or preferrably a (2) compile time
error if the dev forgot to keep something in synch. But it would be best if
it (3) just happened automatically.

One option for (1) would be to use reflection such as

MethodInfo mi = x.GetType().GetMethod("method")
and then display
mi.Name

but this would be the least preferrable of the solutions - (3) is preferred
:)
 
S

sqlguru

Use extensions in the .NET 3.5 framework

public string AsString(this object obj, string methodName)
{
MethodInfo mi = obj.GetType().GetMethod(methodName)
return mi.Name;
}
 
S

sqlguru

Use extensions in the .NET 3.5 framework

public string AsString(this object obj, string methodName)
{
MethodInfo mi = obj.GetType().GetMethod(methodName)
return mi.Name;
}
 
P

proxyuser

Use extensions in the .NET 3.5 framework

public string AsString(this object obj, string methodName)
{
MethodInfo mi = obj.GetType().GetMethod(methodName)
return mi.Name;
}

I was hoping there was something like that built in, but maybe not. I'll
give that a try, thanks.
 
P

proxyuser

Use extensions in the .NET 3.5 framework

public string AsString(this object obj, string methodName)
{
MethodInfo mi = obj.GetType().GetMethod(methodName)
return mi.Name;
}

I was hoping there was something like that built in, but maybe not. I'll
give that a try, thanks.
 
S

sqlguru

Idiot, the OP doesn't want to get the name of the "running method".
Also, the stacktrace is used for debugging purposes!

Use the extension method along with reflection, that is the best
solution and what the OP is asking for.
 
S

sqlguru

Idiot, the OP doesn't want to get the name of the "running method".
Also, the stacktrace is used for debugging purposes!

Use the extension method along with reflection, that is the best
solution and what the OP is asking for.
 
R

Registered User

I want to do something like this

string s = x.method().AsString()

meaning, I'd like to programmatically retrieve the method name (not the name
of the method currently running, but the name of some other known method.)
How is the other known method to be identified if not by name?
The reason I want to do this is I want to display as output to the user the
name of the method being called. I don't want to hard code this value
because if the method name ever changes, I don't want to count on the dev
remembering to change the output string. It would be acceptable to code it
in such a way that it caused a (1) runtime or preferrably a (2) compile time
error if the dev forgot to keep something in synch. But it would be best if
it (3) just happened automatically.

One option for (1) would be to use reflection such as

MethodInfo mi = x.GetType().GetMethod("method")
and then display
mi.Name
This is not going to work because to get the name of the method you
must first know the name of the method.

I have to ask is this information is of any real value to the user?

regards
A.G.
 
R

Registered User

I want to do something like this

string s = x.method().AsString()

meaning, I'd like to programmatically retrieve the method name (not the name
of the method currently running, but the name of some other known method.)
How is the other known method to be identified if not by name?
The reason I want to do this is I want to display as output to the user the
name of the method being called. I don't want to hard code this value
because if the method name ever changes, I don't want to count on the dev
remembering to change the output string. It would be acceptable to code it
in such a way that it caused a (1) runtime or preferrably a (2) compile time
error if the dev forgot to keep something in synch. But it would be best if
it (3) just happened automatically.

One option for (1) would be to use reflection such as

MethodInfo mi = x.GetType().GetMethod("method")
and then display
mi.Name
This is not going to work because to get the name of the method you
must first know the name of the method.

I have to ask is this information is of any real value to the user?

regards
A.G.
 
S

shh

No, it's not what the OP is asking for.  The extension method you posted  
isn't any different from simply using reflection directly, which the OP  
has specifically said is not what he wants to do.

There's no place at all for calling someone an "idiot", but if you're  
going to do it, you ought to at least make sure you're not acting like one  
yourself.

Pete

He is more or less looking for opportunities to name-call me (and
others):
http://groups.google.com/group/micr...amming/browse_thread/thread/e92c300ef2a38bd5#

He is continuing his behavior of calling people idiots and invoking an
ad-hominem argument.
http://groups.google.com/group/microsoft.public.sqlserver.programming/msg/353db1e958137f58?hl=en

I've blocked him from my sender's list, the only reason I knew a post
from him was there was because you replied with a "There's no place at
all for calling someone an 'idiot'", which is the usual (limited
vocabulary) name-calling word used by that particular poster.

....................
 
S

shh

No, it's not what the OP is asking for.  The extension method you posted  
isn't any different from simply using reflection directly, which the OP  
has specifically said is not what he wants to do.

There's no place at all for calling someone an "idiot", but if you're  
going to do it, you ought to at least make sure you're not acting like one  
yourself.

Pete

He is more or less looking for opportunities to name-call me (and
others):
http://groups.google.com/group/micr...amming/browse_thread/thread/e92c300ef2a38bd5#

He is continuing his behavior of calling people idiots and invoking an
ad-hominem argument.
http://groups.google.com/group/microsoft.public.sqlserver.programming/msg/353db1e958137f58?hl=en

I've blocked him from my sender's list, the only reason I knew a post
from him was there was because you replied with a "There's no place at
all for calling someone an 'idiot'", which is the usual (limited
vocabulary) name-calling word used by that particular poster.

....................
 
S

sloan

//Who knows? Maybe he'll grow up and leave his hostile,
unproductive habits behind. Could happen. :)//

It could happen. It could very well happen. I'll keep my (idiot) fingers
crossed!

...........
 
S

sloan

//Who knows? Maybe he'll grow up and leave his hostile,
unproductive habits behind. Could happen. :)//

It could happen. It could very well happen. I'll keep my (idiot) fingers
crossed!

...........
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top