2009-09-01 24 views
14

Tôi đang viết một ứng dụng tạo mã đơn giản để xây dựng POCO từ một lược đồ cơ sở dữ liệu DB2. Tôi biết điều đó không quan trọng, nhưng tôi thích sử dụng bí danh loại thay vì tên loại hệ thống thực tế nếu chúng có sẵn, tức là "int" thay vì "Int32". Có cách nào bằng cách sử dụng sự phản chiếu mà tôi có thể nhận được bí danh của một loại thay vì đó là loại thực tế?Có cách nào để nhận bí danh của loại thông qua phản ánh không?

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

Xin cảm ơn trước.

Trả lời

37

Không - chỉ cần tạo Dictionary<Type,string> để ánh xạ tất cả các loại cho bí danh của chúng. Đó là một bộ cố định, vì vậy không khó để làm:

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

Rats, đó là những gì tôi nghĩ. Ồ, nó không phải là nhiều đánh máy :-) –

+6

Delicious sao chép mì ống. – oscilatingcretin

1

Tôi không nghĩ là có. Bí danh hoàn toàn là một khái niệm thời gian biên dịch cụ thể cho ngôn ngữ .NET dạng paticular mà bạn sử dụng. Một khi bạn phản ánh và xem loại, bạn sẽ thấy đúng loại .NET của đối tượng.

2

Giữ nó đơn giản:

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

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

này không sử dụng phản chiếu, nói đúng, nhưng bạn có thể tới bí danh của loại bằng cách sử dụng 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 

(Có nói rằng, tôi nghĩ rằng các câu trả lời khác cho thấy rằng bạn chỉ cần sử dụng ánh xạ từ các loại CLR đến bí danh C# có lẽ là cách tốt nhất để đi với loại này.)

+3

+1 Thú vị. – sisve

4

Trong trường hợp ai đó cần từ điển có giá trị null:

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

Dựa trên 2 câu trả lời ở trên để sử dụng Từ điển, tôi đã viết 2 phương pháp mở rộng cơ bản có thể giúp làm sạch việc sử dụng một chút. Bao gồm cả lớp này trong dự án của bạn, bạn sẽ có thể sử dụng nó đơn giản bằng cách gọi phương thức Alias ​​() hoặc AliasOrName() trên kiểu như được hiển thị bên dưới.

Ví dụ về cách sử dụng;

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

Việc triển khai;

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; 
} 
Các vấn đề liên quan