C# 中 typeof(int) 和 typeof(Int32) 之间的区别可能吗?

Differentiation between typeof(int) and typeof(Int32) in C# possible?

有一个简单的问题,我想我知道答案但我不想听 来自其他人。

假设我将创建一个通用函数:

public string GetTypeName<T>()
{
}

应该是return类型T的名字,很简单:

return typeof(T).Name

此时我们可以忽略,这对泛型类型不起作用,这不是问题的主题。

我的问题是:如果有人这样调用函数:

GetTypeName<int>()

应该return"int".

如果有人这样调用函数:

GetTypeName<Int32>()

应该return"Int32".

但我认为那是不可能的吧?

感谢您的回答!

无法区分,因为区分不存在

int 是一个关键字,编译器将其解释为 global::System.Int32 类型作为快捷方式或 语法糖 。换句话说,在运行时和 .NET 框架本身内,没有 int 类型,只有 Int32.

这同样适用于所有具有关键字的类型 - 请参阅 MSDN 了解完整列表及其等效项