zszen
zszen

Reputation: 1478

I can't get c# method name by func<T1,T2>

public static string GetFuncName<T1, T2>(Func<T1, T2> func){
    return func.Method.Name;
}

I use this code can get "string function(string)" structure method name.

But I can't get "void function()" structure method name.

"GetFuncName(xxx)" throws volid error.

I need get the "void function()" name finally.

Upvotes: 0

Views: 244

Answers (2)

Cheng Chen
Cheng Chen

Reputation: 43523

You can always get the method name via Delegate.Method.Name. IMO your method is not necessary to be generic.

public static string GetDelegateName(Delegate instance)
{
    return instance.Method.Name;
}

Upvotes: 0

Marco
Marco

Reputation: 23937

For a void you will need an Action<> type delegate and not a Func delegate.

So you will need another method

public static string GetActionName<T1>(Action<T1> action){
    return action.Method.Name;
}

Upvotes: 1

Related Questions