Type t = typeof(bool);
string typeName = t.Name;
在这个简单的示例中,typeName
的值为 "Boolean"
。我想知道我是否/如何让它说 "bool"
。
同样适用于 int/Int32、double/Double、string/String。
最佳答案
using CodeDom;
using Microsoft.CSharp;
// ...
Type t = typeof(bool);
string typeName;
using (var provider = new CSharpCodeProvider())
{
var typeRef = new CodeTypeReference(t);
typeName = provider.GetTypeOutput(typeRef);
}
Console.WriteLine(typeName); // bool
关于c# - 通过反射获取简单类型的用户友好名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4615553/