views:

315

answers:

2

Does anyone know of any existing functionality in the .NET BCL to print the full signature of a method at runtime (like what you'd see in the VS ObjectBrowser - including parameter names) using information available from MethodInfo?

So for example, if you look up String.Compare() one of the overloads would print as:

public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, System.Globalization.CultureInfo culture)

Note the presence of the complete signature with all access and scope qualifiers as well as a complete list of parameters including names. This is what I'm looking for. I could write my own method, but I would rather use an existing implementation if possible.

+1  A: 

Unfortunately I don't believe there is a built in method that would do that. Your best be would be to create your own signature by investigating the MethodInfo class

EDIT: I just did this

 MethodBase mi = MethodInfo.GetCurrentMethod();
 mi.ToString();

and you get

Void Main(System.String[])

This might not be what you're looking for, but it's close.

How about this

 public static class MethodInfoExtension
        {
            public static string MethodSignature(this MethodInfo mi)
            {
                String[] param = mi.GetParameters()
                              .Select(p => String.Format("{0} {1}",p.ParameterType.Name,p.Name))
                              .ToArray();


            string signature = String.Format("{0} {1}({2})", mi.ReturnType.Name, mi.Name, String.Join(",", param));

            return signature;
        }
    }

    var methods = typeof(string).GetMethods().Where( x => x.Name.Equals("Compare"));

    foreach(MethodInfo item in methods)
    {
        Console.WriteLine(item.MethodSignature());
    }

This is the result

Int32 Compare(String strA,Int32 indexA,String strB,Int32 indexB,Int32 length,StringComparison comparisonType)

Stan R.
Thanks. Unfortunately, my use case does require the parameter names to the method, which MethodInfo.ToString() doesn't emit.
LBushkin
yes it also does not display Method Attributes.
Stan R.
A: 

Checkout the GetParameters() method on MethodBase. That will give you the info about the parameters including the name of the parameter. I don't believe a pre-existing method exists to print out the name but using the ParameterInfo[] to build that should be trivial.

How about this:

public string GetSignature(MethodInfo mi)
{
if(mi == null)
 return "";
StringBuilder sb = new StringBuilder();

if(mi.IsPrivate)
 sb.Append("private ");
else if(mi.IsPublic)
 sb.Append("public ");
if(mi.IsAbstract)
 sb.Append("abstract ");
if(mi.IsStatic)
 sb.Append("static ");
if(mi.IsVirtual)
 sb.Append("virtual ");

sb.Append(mi.ReturnType.Name + " ");

sb.Append(mi.Name + "(");

String[] param = mi.GetParameters()
  .Select(p => String.Format(
              "{0} {1}",p.ParameterType.Name,p.Name))
        .ToArray();


sb.Append(String.Join(", ",param));

sb.Append(")");

return sb.ToString();

}

Stephan