Is there a way to get a type's alias through reflection?

Related searches

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

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

type and isinstance in Python, Python have a built-in method called as type which generally come in One elementary error people make is using the type() function where� Allowing yourself to type incorrectly will actually reinforce your bad habits and common mistakes! Slow your typing pace until you can attain 100% accuracy. If you come across a difficult word, slow down further to type it properly.

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.)

Handbook - Advanced Types, instanceof type guards are a way of narrowing types using their constructor TypeScript has two special types, null and undefined , that have the values null� Google Docs Voice Typing lets you speak instead of type. By Todd Ogasawara Voice Typing does not have a way to correct or change text using just your voice. there is no punctuation, since

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

How to Lower Blood Sugar Quickly in an Emergency: Tips and More, Here's how to lower blood glucose, when to go to the emergency room, If you have type 1 diabetes, your doctor will likely recommend you� You can also load an assembly using the Assembly.Load method, and then use the Assembly.GetType or Assembly.GetTypes method to get Type objects. If a type is in an assembly known to your program at compile time, it is more efficient to use typeof in C# or the GetType operator in Visual Basic.

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

The Basics — The Swift Programming Language (Swift 5.3), Here's an example of how constants and variables can be used to track the number of However, this doesn't mean that you have to specify the type of every� Tax Account Transcript - shows basic data such as return type, marital status, adjusted gross income, taxable income and all payment types. It also shows changes made after you filed your original return. This transcript is available for the current tax year and up to 10 prior years using Get Transcript Online or Form 4506-T.

public string GetAlias(Type t)
{
    string typeName = "";
    using (var provider = new CSharpCodeProvider())
    {
        var typeRef = new CodeTypeReference(t);
        typeName = provider.GetTypeOutput(typeRef);
    }
    return typeName;
}

Genetics of Diabetes, If you have this syndrome, your child's risk of getting the syndrome—including type 1 diabetes—is 1 in 2. Researchers are learning how to predict a person's odds� There is another simpler way to check whether your hard drive is HDD or SSD. You just have to open the Drive Optimizer tool in Windows which shows the disk type while listing the drives in the system.

My hand is placed on the end of the chair if anybody is wondering. Just a bit of a gaff from me there on camera. But yes, this is my second and final (for no

This actually depends on the type of USB port you have on your laptop. USB Type A is a non-starter If you are you currently using a wired mouse or wired keyboard and you unplug one of them from you’ll see that there’s a rectangular connector at the end of the cord; the metal piece that you insert into the computer.

If you’re working on a Mac and want to use Preview instead, select the Annotate option from the Tools menu. Click on text, which will cause a text box to appear, and drag it to the correct position. When you’re ready to type in your text, click on the A icon to bring up a dialogue box, select text, and enter your text.

Comments
  • @MikaelDúiBolinder I asked this question before the question you are referencing.
  • Ah, flagged the other one instead now.
  • Rats, that's what I thought. Oh well, it's not that much typing :-)