假设您有一个与myMethod方法相关的MethodInfo:
void myMethod(int param1, int param2) { }并且您希望创建一个表示方法签名的字符串:
string myString = "myMethod (int, int)";通过遍历MethodInfo参数,我能够通过调用参数类型的ToString方法来实现这些结果:
"myMethod (System.Int32, System.Int32)"我如何改进这一点并产生上面所示的结果?
发布于 2012-05-19 13:41:53
发布于 2012-05-19 03:28:33
据我所知,没有内置的东西可以将原语(System.Int32)的真实类型名转换为内置的别名(int)。由于这些别名的数量非常少,因此编写自己的方法并不太难:
public static string GetTypeName(Type type)
{
if (type == typeof(int)) // Or "type == typeof(System.Int32)" -- same either way
return "int";
else if (type == typeof(long))
return "long";
...
else
return type.Name; // Or "type.FullName" -- not sure if you want the namespace
}也就是说,如果用户确实输入了System.Int32而不是int (当然这是完全合法的),这种技术仍然会打印出"int“。您对此无能为力,因为无论哪种方式,System.Type都是相同的--所以您无法找出用户实际输入的是哪个变体。
https://stackoverflow.com/questions/10658563
复制相似问题