有没有一种方法,通过反射来获取一个类型的别名? [英] Is there a way to get a type's alias through reflection?

查看:248
本文介绍了有没有一种方法,通过反射来获取一个类型的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个简单的code生成应用程序从一个DB2数据库架构建立POCO的。我知道这并不重要,但我preFER使用类型别名,而不是它们是否可用,即INT,而不是实际的系统类型名称的Int32。有没有使用反射的方式,我可以得到一个类型的别名,而不是它的实际类型?

I'm writing a simple code generation application to build POCO's from a DB2 database schema. I know it doesn't matter, but I prefer to use type aliases rather than the actual system type name if they are available, i.e., "int" rather than "Int32." Is there a way using reflection that I can get a type's alias rather than it's the actual type?

//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; }

在此先感谢。

Thanks in advance.

推荐答案

都能跟得上 - 只需创建一个词典<型号,串> 所有类型的映射到它们的别名。这是一组固定的,所以它不是很难做到的:

Nope - just create a Dictionary<Type,string> to map all of the types to their aliases. It's a fixed set, so it's not hard to do:

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" }
};

这篇关于有没有一种方法,通过反射来获取一个类型的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆