Programmatically get type alias in .NET

Is it possible to define an alias (s) of an object type through reflection? In the following example, myObject is of type System.Int32 - for example,

Type t = myObject.GetType();

the so-called will be int32 . However, I would like to identify possible aliases, if any, of the objects in question, so that I can resolve an int type name in addition to Int32

I'm not going to spend a huge amount of time on this. If there is no extremely simple inline solution, I will simply match the values ​​myself. =

+3
source share
3 answers

. " " ( ) "int" - , .

, , look ups . API , .

+5

CodeDom

    var cs = new CSharpCodeProvider();
    var vb = new VBCodeProvider();

    var type = typeof (int);
    Console.WriteLine("Type Name: {0}", type.Name); // Int32
    Console.WriteLine("C# Type Name: {0}", cs.GetTypeOutput(new CodeTypeReference(type))); // int
    Console.WriteLine("VB Type Name: {0}", vb.GetTypeOutput(new CodeTypeReference(type))); // Integer
+5

Aliases seem to be just compilation time. Note how they are not applied outside the specified source file. It’s just convenience, nothing more.

0
source

Source: https://habr.com/ru/post/1758086/


All Articles