代码之家  ›  专栏  ›  技术社区  ›  AJ.

有没有办法通过反射来获取类型的别名?

  •  17
  • AJ.  · 技术社区  · 15 年前

    我正在编写一个简单的代码生成应用程序,从DB2数据库模式构建POCO。我知道这并不重要,但是我更喜欢使用类型别名,而不是实际的系统类型名称(如果可用),即“int”而不是“int32”。是否有一种使用反射的方法可以获得类型的别名而不是实际的类型?

    //Get the type name
    var typeName = column.DataType.Name;
    
    //If column.DataType is, say, Int64, I would like the resulting property generated
    //in the POCO to be...
    
    public long LongColumn { get; set; }
    
    //rather than what I get now using the System.Reflection.MemberInfo.Name property:
    
    public Int64 LongColumn { get; set; }
    

    事先谢谢。

    7 回复  |  直到 8 年前
        1
  •  45
  •   Jon Skeet    11 年前

    不-创建一个 Dictionary<Type,string> 将所有类型映射到它们的别名。这是一个固定的集合,所以不难做到:

    private static readonly Dictionary<Type, string> Aliases =
        new Dictionary<Type, string>()
    {
        { typeof(byte), "byte" },
        { typeof(sbyte), "sbyte" },
        { typeof(short), "short" },
        { typeof(ushort), "ushort" },
        { typeof(int), "int" },
        { typeof(uint), "uint" },
        { typeof(long), "long" },
        { typeof(ulong), "ulong" },
        { typeof(float), "float" },
        { typeof(double), "double" },
        { typeof(decimal), "decimal" },
        { typeof(object), "object" },
        { typeof(bool), "bool" },
        { typeof(char), "char" },
        { typeof(string), "string" },
        { typeof(void), "void" }
    };
    
        2
  •  28
  •   LukeH    13 年前

    严格来说,这不使用反射,但可以通过使用codedom来访问类型的别名:

    Type t = column.DataType;    // Int64
    
    string typeName;
    using (var provider = new CSharpCodeProvider())
    {
        var typeRef = new CodeTypeReference(t);
        typeName = provider.GetTypeOutput(typeRef);
    }
    
    Console.WriteLine(typeName);    // long
    

    (尽管如此,我认为其他建议您只使用从clr类型到c别名的映射的答案可能是最好的方法。)

        3
  •  8
  •   rubenmch    9 年前

    如果有人需要可为空的字典:

    private static readonly Dictionary<Type, string> Aliases = new Dictionary<Type, string>()
        {
            { typeof(byte), "byte" },
            { typeof(sbyte), "sbyte" },
            { typeof(short), "short" },
            { typeof(ushort), "ushort" },
            { typeof(int), "int" },
            { typeof(uint), "uint" },
            { typeof(long), "long" },
            { typeof(ulong), "ulong" },
            { typeof(float), "float" },
            { typeof(double), "double" },
            { typeof(decimal), "decimal" },
            { typeof(object), "object" },
            { typeof(bool), "bool" },
            { typeof(char), "char" },
            { typeof(string), "string" },
            { typeof(void), "void" },
            { typeof(Nullable<byte>), "byte?" },
            { typeof(Nullable<sbyte>), "sbyte?" },
            { typeof(Nullable<short>), "short?" },
            { typeof(Nullable<ushort>), "ushort?" },
            { typeof(Nullable<int>), "int?" },
            { typeof(Nullable<uint>), "uint?" },
            { typeof(Nullable<long>), "long?" },
            { typeof(Nullable<ulong>), "ulong?" },
            { typeof(Nullable<float>), "float?" },
            { typeof(Nullable<double>), "double?" },
            { typeof(Nullable<decimal>), "decimal?" },
            { typeof(Nullable<bool>), "bool?" },
            { typeof(Nullable<char>), "char?" }
        };
    
        4
  •  3
  •   Pang Ajmal PraveeN    8 年前
    public string GetAlias(Type t)
    {
        string typeName = "";
        using (var provider = new CSharpCodeProvider())
        {
            var typeRef = new CodeTypeReference(t);
            typeName = provider.GetTypeOutput(typeRef);
        }
        return typeName;
    }
    
        5
  •  2
  •   jason    15 年前

    保持简单:

    var aliasDict = new Dictionary<Type, string>() {
        { typeof(int), "int" },
        { typeof(long), "long" },
        // etc
    }
    
    Type reflectedType;
    string aliasedTypeName = aliasDict[reflectedType];
    
        6
  •  2
  •   mbrdev    8 年前

    基于上面关于使用字典的两个答案,我编写了两个基本的扩展方法,可以帮助清理使用。在您的项目中包含这个类,您可以通过对类型调用alias()或aliasorname()方法来使用它,如下所示。

    示例用法;

            // returns int
            string intAlias = typeof(Int32).Alias();
            // returns int
            string intAliasOrName = typeof(Int32).AliasOrName();
            // returns string.empty
            string dateTimeAlias = typeof(DateTime).Alias();
            // returns DateTime
            string dateTimeAliasOrName = typeof(DateTime).AliasOrName();
    

    执行情况;

    public static class TypeExtensions
    {
        public static string Alias(this Type type)
        {
            return TypeAliases.ContainsKey(type) ?
                TypeAliases[type] : string.Empty;
        }
    
        public static string AliasOrName(this Type type)
        {
            return TypeAliases.ContainsKey(type) ?
                TypeAliases[type] : type.Name;
        }
    
        private static readonly Dictionary<Type, string> TypeAliases = new Dictionary<Type, string>
        {
            { typeof(byte), "byte" },
            { typeof(sbyte), "sbyte" },
            { typeof(short), "short" },
            { typeof(ushort), "ushort" },
            { typeof(int), "int" },
            { typeof(uint), "uint" },
            { typeof(long), "long" },
            { typeof(ulong), "ulong" },
            { typeof(float), "float" },
            { typeof(double), "double" },
            { typeof(decimal), "decimal" },
            { typeof(object), "object" },
            { typeof(bool), "bool" },
            { typeof(char), "char" },
            { typeof(string), "string" },
            { typeof(void), "void" },
            { typeof(byte?), "byte?" },
            { typeof(sbyte?), "sbyte?" },
            { typeof(short?), "short?" },
            { typeof(ushort?), "ushort?" },
            { typeof(int?), "int?" },
            { typeof(uint?), "uint?" },
            { typeof(long?), "long?" },
            { typeof(ulong?), "ulong?" },
            { typeof(float?), "float?" },
            { typeof(double?), "double?" },
            { typeof(decimal?), "decimal?" },
            { typeof(bool?), "bool?" },
            { typeof(char?), "char?" }
        };
    }
    
        7
  •  1
  •   Matthew Manela    15 年前

    我想没有。别名是一个完整的编译时概念,特定于您使用的pattical.net语言。一旦您反映和查看类型,您将看到对象的真正.NET类型。