Extensions: Getting the Signature of a Method When it is Used as a Parameter Func

If you’ve passed a method into another one as a Func parameter and need to get the original method’s signature you can do so like this:

[csharp] public static string GetMethodSignature<TObject>(this Func<TObject> method)
{
var signature = string.Empty;
var info = method.GetMethodInfo();

var sb = new StringBuilder();
sb.AppendFormat("{0} {1}", info.ReturnType.Name, info.Name);
if (info.IsGenericMethod)
{
sb.AppendFormat("<{0}>", string.Join(", ", info.GetGenericArguments().Select(x => x.Name)));
}

sb.AppendFormat(
"({0})",
string.Join(
", ",
info.GetParameters().Select(x => string.Format("{0} {1}", x.ParameterType.Name, x.Name))));

return signature;
}[/csharp]

Leave a Reply

Your email address will not be published. Required fields are marked *