Is there a way to get a type's alias through r

2019-01-06 23:13发布

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.

7条回答
闹够了就滚
2楼-- · 2019-01-06 23:33
public string GetAlias(Type t)
{
    string typeName = "";
    using (var provider = new CSharpCodeProvider())
    {
        var typeRef = new CodeTypeReference(t);
        typeName = provider.GetTypeOutput(typeRef);
    }
    return typeName;
}
查看更多
Animai°情兽
3楼-- · 2019-01-06 23:41

I don't think there is. The alias is completly a compile time concept specific to the paticular .NET language you use. Once you reflect and view the type you will see the true .NET type of the object.

查看更多
劫难
4楼-- · 2019-01-06 23:41

Based on the above 2 answers for using a Dictionary, I've written 2 basic extension methods that might help clean up the use a bit. Including this class in your project you will be able to use it simply by calling the Alias() or AliasOrName() methods on the type as shown below.

Example usage;

        // 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();

The implementation;

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?" }
    };
}
查看更多
男人必须洒脱
5楼-- · 2019-01-06 23:47

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" }
};
查看更多
我命由我不由天
6楼-- · 2019-01-06 23:48

Keep it simple:

var aliasDict = new Dictionary<Type, string>() {
    { typeof(int), "int" },
    { typeof(long), "long" },
    // etc
}

Type reflectedType;
string aliasedTypeName = aliasDict[reflectedType];
查看更多
我命由我不由天
7楼-- · 2019-01-06 23:54

This doesn't use reflection, strictly speaking, but you can get to the type's alias by using 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

(Having said that, I think that the other answers suggesting that you just use a mapping from CLR types to C# aliases are probably the best way to go with this one.)

查看更多
登录 后发表回答