C#,反射和基元类型
可能重复:C#,反射和基元类型,c#,reflection,primitive-types,C#,Reflection,Primitive Types,可能重复: 我在C#中有以下代码: 此代码输出: 布尔、字节、字符、双精度、Int16、Int32、Int64、IntPtr、SByte、, 单,UInt16,UInt32,UInt64,UIntPtr 如果可能的话,我想用bool替换Boolean,Byte替换Byte等等,而不依赖固定数组或字典。有什么方法可以做到这一点吗?这是重复的 这也是双向飞碟的一个好答案 答案是,你可以,而且不用字典 Type t = typeof(bool); string typeName; using
我在C#中有以下代码: 此代码输出: 布尔、字节、字符、双精度、Int16、Int32、Int64、IntPtr、SByte、, 单,UInt16,UInt32,UInt64,UIntPtr 如果可能的话,我想用
bool
替换Boolean
,Byte
替换Byte
等等,而不依赖固定数组或字典。有什么方法可以做到这一点吗?这是重复的
这也是双向飞碟的一个好答案
答案是,你可以,而且不用字典
Type t = typeof(bool);
string typeName;
using (var provider = new CSharpCodeProvider())
{
var typeRef = new CodeTypeReference(t);
typeName = provider.GetTypeOutput(typeRef);
}
Console.WriteLine(typeName); // bool
它就像一个符咒。谢谢
Type t = typeof(bool);
string typeName;
using (var provider = new CSharpCodeProvider())
{
var typeRef = new CodeTypeReference(t);
typeName = provider.GetTypeOutput(typeRef);
}
Console.WriteLine(typeName); // bool