Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert C# method name to text

Tags:

c#

I am using a generic handler in Visual Studio 2013.

What I am trying to do is to create a URL that incorporates the name of a method, but I want the name of the method to be real code so that it will not be hard-coded and follow along if the function name was changed.

If I were doing this in C or C++, I would have just said:

#define GENERATE_REFERENCE(text) #text

I don't really care it is formed as a method call as I have prototyped here

"Pseudo-code" in C# of what I am trying to do:

public class MyClass {
    public void SayHello (String name)
    {
    ...
    }

    public void GenerateLink()
    {
         url = "... "+GenerateReference(this.SayHello);
         // url would be "... SayHello";

    }
    public String GenerateReference( DataType method )
    {
         // Is there some way to do this?
         return method.MethodName.ToString();
    }
}

My question is different than the suggested duplicate question get methodinfo from a method reference C# because my question comes from a place of great ignorance of C# mechanisms (neophyte). The suspected duplicate question implies a much higher level of understanding well beyond what I demonstrated in my question - I didn't know enough to ask that question. I would have never have found this answer from my searching.

like image 471
craigdfrench Avatar asked Nov 10 '15 17:11

craigdfrench


People also ask

How do you convert C to F fast?

To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.

How do you convert to Celsius?

In other words, if you'd like to convert a temperature reading in Fahrenheit to Celsius: Start with the temperature in Fahrenheit (e.g., 100 degrees). Subtract 32 from this figure (e.g., 100 - 32 = 68). Divide your answer by 1.8 (e.g., 68 / 1.8 = 37.78)

What is Celsius vs Fahrenheit?

The Conversion This means Celsius is 1.8 times larger than Fahrenheit. To put another way, 1 degree Fahrenheit is 5/9 degree Celsius. Despite the pretty large differences between one another, these two temperature scales intersect at -40 degrees, meaning that -40 degrees Fahrenheit is the same as -40 degrees Celsius.

What temperature in Fahrenheit is 50 C?

Answer: 50° Celsius is equal to 122° Fahrenheit.


2 Answers

C# 6

nameof(MyClass.SayHello)

Before C# 6

public static String GenerateReference<T>(Expression<Action<T>> expression)
{
    var member = expression.Body as MethodCallExpression;

    if (member != null)
        return member.Method.Name;

    throw new ArgumentException("Expression is not a method", "expression");
}

GenerateReference<MyClass>(c => c.SayHello(null));

Credit goes to https://stackoverflow.com/a/9382501/471321

like image 84
Steven Wexler Avatar answered Sep 25 '22 04:09

Steven Wexler


c# 6 introduces a new operator called nameof which eliminates these hardcoded string with method names.

You can use it as follows: nameof(Class.Method)

like image 39
Arthur Rizzo Avatar answered Sep 24 '22 04:09

Arthur Rizzo