Reputation: 318
I have a function which takes a function pointer
like
public void myfunc<a,b>(Func<a,b> functionpointer)
{
String functionname;
// Do some magic to get functionpointers name and set it to functionname
}
Is it possible to get the function's name without running it?
I know that you can get the function name of the currently running function, but how do you get the name of a function that you're going to call?
Forgive me if such a question might have been posted before, i can't find a solution for it in C#
Upvotes: 5
Views: 982
Reputation: 1765
Use Reflection
foreach (Type objType in assembly.GetTypes())
{
//List<string> listInner = new List<string>();
var listInner = new HashSet<string>();
listInner.Add(objType.FullName);
foreach (MemberInfo obMember in objType.GetMembers())
{
listInner.Add(obMember.MemberType + " " + obMember.ToString());
}
}
Upvotes: 0
Reputation: 116108
public void myfunc<a, b>(Expression<Func<a, b>> expr)
{
Func<a, b> functionpointer = expr.Compile();
String functionname = "";
var mce = expr.Body as MethodCallExpression;
if(mce!=null)
{
functionname = mce.Method.Name;
}
}
Upvotes: 0
Reputation: 223207
You can use MemberInfo.Name Property
string functionname = functionpointer.Method.Name;
Upvotes: 7