Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of function in c#

Tags:

c#

unity3d

In Unity when using coroutines or InvokeRepeating, you have to give a string with the name of the function you want to call. Though this is a pain if you change the name of that function, since you have to remember to change the coroutines that use it. Is there a cleaner way of doing this?

Currently it looks like this:

InvokeRepeating ("SendChangedValues", SEND_RATE, SEND_RATE);

though it would be nice to have something like

InvokeRepeating (SendChangedValues.Name(), SEND_RATE, SEND_RATE); //or
InvokeRepeating (functions.GetName(SendChangedValues), SEND_RATE, SEND_RATE);

Is this possible in c#? Or something else that makes sure I get an error/warning when I change the function's name without changing those strings.

Edit 1: The cleanest thing I could think of is making a const string with the function's name, and putting it just before the function itself. So it's harder to forget to change the string, since it's right there above it, and I also only have to change that one const string to change all the coroutines.

Thanks!

like image 802
The Oddler Avatar asked Apr 08 '14 18:04

The Oddler


People also ask

What is function name C?

What type is a function name in C? A function name or function designator has a function type. When it is used in an expression, except when it is the operand of sizeof or & operator, it is converted from type "function returning type" to type "pointer to a function returning type".

How do I get the name of a function in C++?

You cannot get the name of the function in C++, but you can print the pointer and later check the binary (if not stripped) for the function name. The signature can be printed exactly as you are doing, just that the type name is not really 'human readable'.

Can a function have any name in C?

Every function has a name by which it is known to the rest of the program. The name of a function in C can be anything from a single letter to a long word. The ANSI Standard, however, only guarantees that C will be able to distinguish the first 31 letters of identifiers , or function and variable names.

What is __ Pretty_function __?

The identifier __PRETTY_FUNCTION__ holds the name of the function pretty printed in a language specific fashion. These names are always the same in a C function, but in a C++ function they may be different. For example, this program: extern "C" { extern int printf (char *, ...


1 Answers

ahhh.. if it were next the C# version, you could have used the nameof operator.

for now, Does this help your cause?

private static string GetFunctionName(Action method)
{
 return method.Method.Name;
}

called using:

string methodName = GetFunctionName(SendChangedValues);

you might want to explore different delegate types.. Action, Func etc.

the only problem with above is that for every method signature, you might need to define the right signature/overloads to get name.

like image 164
Raja Nadar Avatar answered Oct 18 '22 21:10

Raja Nadar