2009-09-01 75 views
14

我正在編寫一個簡單的代碼生成應用程序來從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; } 

在此先感謝。

回答

37

不 - 只需創建一個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" } 
}; 
+0

老鼠,這就是我的想法。哦,這不是那麼多打字:-) – 2009-09-01 14:53:18

+6

美味的複製意大利麪。 – oscilatingcretin 2014-04-22 12:05:45

1

我不認爲有。這個別名完全是一個特定於你所使用的特定.NET語言的編譯時概念。一旦你反思和查看類型,你將看到對象的真正的.NET類型。

2

保持簡單:

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

Type reflectedType; 
string aliasedTypeName = aliasDict[reflectedType]; 
24

這不使用反射,嚴格地說,但你可以通過使用的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

+1有趣。 – sisve 2009-09-01 16:29:04

4

如果有人需要可空字典:

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

根據以上2個使用詞典的答案,我寫了2個基本的擴展方法,可能有助於清理使用一下。在你的項目中包含這個類,你可以簡單地通過調用類型的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?" } 
    }; 
} 
1
public string GetAlias(Type t) 
{ 
    string typeName = ""; 
    using (var provider = new CSharpCodeProvider()) 
    { 
     var typeRef = new CodeTypeReference(t); 
     typeName = provider.GetTypeOutput(typeRef); 
    } 
    return typeName; 
} 
相關問題