2009-09-01 8 views
14

Piszę prostą aplikację generującą kod do budowania POCO ze schematu bazy danych DB2. Wiem, że to nie ma znaczenia, ale wolę używać aliasy typu zamiast rzeczywistej nazwy typu systemu, jeśli są dostępne, tj. "Int" zamiast "Int32". Czy istnieje sposób użycia refleksji, że mogę uzyskać alias typu, a nie jego rzeczywisty typ?Czy istnieje sposób na uzyskanie aliasu typu poprzez odbicie?

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

Z góry dziękuję.

Odpowiedz

37

Nie - wystarczy utworzyć Dictionary<Type,string>, aby odwzorować wszystkie typy na ich aliasy. Jest to ustalony zestaw, więc nie jest trudne:

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

Szczury, że to, co myślę. No cóż, to nie jest tak dużo pisania :-) –

+6

Pyszne makaron kopii. – oscilatingcretin

1

Nie sądzę, że istnieje. Alias ​​jest całkowicie koncepcją czasu kompilacji specyficzną dla konkretnego języka .NET, którego używasz. Po odbiciu i wyświetleniu typu zobaczysz prawdziwy typ .NET obiektu.

2

Keep it simple:

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

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

nie użyć refleksji, ściśle mówiąc, ale można dostać się do aliasu na typ za pomocą 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 

(Mimo, że ja myślę, że inne odpowiedzi sugerujące, że używasz mapowania z typów CLR do aliasów C#, to prawdopodobnie najlepszy sposób, aby przejść z tym.)

+3

+1 Interesujące. – sisve

4

Jeśli ktoś potrzebuje słownika z zerami:

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

Opierając się na powyższych 2 odpowiedziach dotyczących używania słownika, napisałem dwie podstawowe metody rozszerzenia, które mogą nieco pomóc w wyczyszczeniu użycia. Uwzględnienie tej klasy w twoim projekcie będzie można z niej skorzystać po prostu wywołując metody Alias ​​() lub AliasOrName() na typie, jak pokazano poniżej.

Przykładowe zastosowanie;

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

Wdrożenie;

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; 
} 
Powiązane problemy