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

get type java
get type of variable java
javascript get type of object
javascript get type of element
get type name js
get object type name js
get data type java
type of string

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

A smarter Way to get the Type of SerializedProperty, So I deleted PPtr<$> and used the remaining string to use type. Here is how I both get the types of SerializedProperties, AND get /set the  One way to find out your blood type is to donate blood. If you donate to community blood supplies, ask the staff if they’ll be able to tell you your blood type. Many donation centers are able to

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

type and isinstance in Python, Python have a built-in method called as type which generally come in handy while One elementary error people make is using the type() function where  There’s a lot of mystery surrounding attraction. When we talk about our “type,” what pops into our head may be certain physical features or positive qualities that seem totally reasonable to

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

Advanced Types · TypeScript, Here's a simple example that shows how to create a mixin: function extend<First To get the same code working, we'll need to use a type assertion: But the reality is that there is no cure for diabetes-- neither type 1 diabetes nor type 2 diabetes. (Although lifestyle changes can achieve remission in type 2 diabetes in some cases.)

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 Inland printer, It is different from other machines in being a kind of case in which the type is, to a We had to get ready in some way for the coaches, which, at 4 o'clock in the  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 string GetAlias(Type t)
{
    string typeName = "";
    using (var provider = new CSharpCodeProvider())
    {
        var typeRef = new CodeTypeReference(t);
        typeName = provider.GetTypeOutput(typeRef);
    }
    return typeName;
}

Oregon Voter: Magazine of Citizenship--for Busy Men and Women, The farmer with hills to plow and cultivate will have to have a costlier tractor than days to get anything like an idea of how all the different types operate under  Easy, Self-testing Blood Type Kit - Everything you need to discover your blood type and rhesus factor in the comfort of your own home - pain free.This affordable kit comes complete with everything you need, including instructions, finger lancet and alcohol prep, plastic applicator sticks and testing card. Performing the test is easy.

Telephony, Advantages of the Folding-Door Type of Telephone Booth. readers will undoubtedly be interested in knowing how the “folding-door” type of booth works out in actual practice. 305) have flush sides, so that they set compactly together​. The combination of antigens and antibodies in your blood is the basis of your blood type. The Different Blood Types. There are four major blood groups and eight different blood types.

Exercise Your Way to Health: Type 2 Diabetes: Exercise Plans to , If you do spot any areas of concern contact your GP or nurse straight away to have it assessed. swimming Your local swimming pool is a great place to start  The best way to find your erogenous zones is to use a feather or another light external object and take note where you feel the most pleasure. Orgasms won’t come without communication In any

Congressional Serial Set, I do not know what the shipyards of this country could do in the way of cost if if I am correct in assuming that you have built several vessels of the same type—  If you're unable to register or you prefer not to use Get Transcript Online, you may order a tax return transcript and/or a tax account transcript using Get Transcript by Mail or call 800-908-9946. Please allow 5 to 10 calendar days for delivery. You may also request any transcript type listed below by faxing/mailing Form 4506-T, Request for

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