2012-04-26 90 views
4

可能重复:
How can I get the primitive name of a type in C#?C#,反射和原始类型

我在C#以下代码:

 Assembly sysAssembly = 0.GetType().Assembly; 
     Type[] sysTypes = sysAssembly.GetTypes(); 
     foreach (Type sysType in sysTypes) 
     { 
      if (sysType.IsPrimitive && sysType.IsPublic) 
       Console.WriteLine(sysType.Name); 
     } 

该代码输出:

布尔,字节,字符,双,Int16的,的Int32,Int64的,IntPtr的,为SByte, 单,UINT16,UInt32的,UINT64,UIntPtr,

我想通过bool通过byte和更换BooleanByte所以在可能的时候,不依赖于固定的数组或字典。有没有办法做到这一点?

回答

6

这是一个重复,以

C# - Get user-friendly name of simple types through reflection?

这是飞碟双向一个很好的答案,也

How can I get the primitive name of a type in C#?

答案是,你可以,并没有字典。

Type t = typeof(bool); 

string typeName; 
using (var provider = new CSharpCodeProvider()) 
{ 
    var typeRef = new CodeTypeReference(t); 
    typeName = provider.GetTypeOutput(typeRef); 
} 

Console.WriteLine(typeName); // bool 
+0

它像一个魅力。谢谢。 – 2012-04-26 17:39:00