有谁知道在.net BCL的任何现有的功能在运行时打印方法的完整签名(像什么你在VS ObjectBrowser看到的 - 包括参数名称)?使用可从MethodInfo的信息。
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?
因此,例如,如果你查找的String.Compare()的一个重载将打印为:
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)
请注意完整的签名与所有访问和范围预选赛presence以及参数,包括名称的完整列表。这就是我要找的。我可以写我自己的方法,但我宁愿使用现有的实现,如果可能的。
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.
不幸的是,我不相信有一个内置的方法,将做到这一点。你最好是将受调查的MethodInfo类来创建自己的签名
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
编辑:我只是做了这个
MethodBase mi = MethodInfo.GetCurrentMethod();
mi.ToString();
和你
无效的主要(System.String [])
Void Main(System.String[])
这可能不是你要找的内容,但它很接近。
This might not be what you're looking for, but it's close.
这个怎么样
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());
}
这是结果
的Int32比较(字符串STRA,的Int32 索引A,字符串STRB,的Int32 indexB,的Int32 长度,StringComparison comparisonType)
Int32 Compare(String strA,Int32 indexA,String strB,Int32 indexB,Int32 length,StringComparison comparisonType)