C#、反射和原始类型

标签 c# reflection primitive-types

<分区>

Possible Duplicate:
How can I get the primitive name of a type in 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);
        }

这段代码输出:

Boolean, Byte, Char, Double, Int16, Int32, Int64, IntPtr, SByte, Single, UInt16, UInt32, UInt64, UIntPtr,

如果可能的话,我想用 bool 替换 Boolean,用 byte 替换 Byte 等等,没有依赖固定的数组或字典。有办法做到这一点吗?

最佳答案

这是一个副本

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

这也是 Skeet 的一个很好的回答

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

关于C#、反射和原始类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10338290/

相关文章:

c# - Entity Framework +MySQL类生成

reflection - reflect.StructOf 在添加到 633ab74 之前存在什么替代方案?

php - Python:将 args 转换为 dict,就像 PHP 的 compact 一样?

c# - Java 中的类型

c# - 转换为原始类型并用作参数?

c# - 如何在 GridView 行悬停上显示工具提示

c# - 如何获取Elasticsearch评分结果详细信息?

java - 如何判断原始类型是否被转换为 Java 中的对象?

c# - 如何将 DBNull 分配给原始类型以方便数据库插入

c# - 查找链接元素