Is there a way to get a type's alias through reflection?
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" }
};
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.)
In case someone needs the dictionary with nullables:
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?" }
};