假设您有一个与myMethod的方法有关的方法,如myMethod:

void myMethod(int param1, int param2) { }
.

,您希望创建一个表示该方法签名的字符串:

string myString = "myMethod (int, int)";
.

循环通过方法,我能够通过调用参数类型的ToString方法来实现这些结果:

"myMethod (System.Int32, System.Int32)"
.

如何改进此并产生上面显示的结果?

其他提示

据我所知,没有内置的,它将将原始(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
}
.

所说,如果用户实际上 did 类型,则在生成的,而不是世纪odicetagcode(当然是完全合法的),这种技术仍将打印出“int”。没有一个很多,你可以做到这一点,因为System.Int32是相同的方式 - 所以你没有办法找出用户实际键入的哪个变体。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top