Ala*_*ito 4 c# reflection primitive-types
可能重复:
如何在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);
}
Run Code Online (Sandbox Code Playgroud)
此代码输出:
Boolean,Byte,Char,Double,Int16,Int32,Int64,IntPtr,SByte,Single,UInt16,UInt32,UInt64,UIntPtr,
我想在可能的情况下替换Booleanby bool,Byteby byte等,而不依赖于固定的数组或字典.有没有办法做到这一点?
这是重复的
这也是Skeet的一个很好的答案
答案是,你可以,没有字典.
Type t = typeof(bool);
string typeName;
using (var provider = new CSharpCodeProvider())
{
var typeRef = new CodeTypeReference(t);
typeName = provider.GetTypeOutput(typeRef);
}
Console.WriteLine(typeName); // bool
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2291 次 |
| 最近记录: |