Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get c#-specific name for a given framework System.Type?

Tags:

c#

types

I have a Type (via reflection, for example).

I have the value of the Name property on, for example, String... that's "System.String". I want to see "string" ("int" instead of "System.Int32", etc, etc).

Can anything in the framework (or the language) give me that? Can I convert a Framework type name to a language type name (or, alternatively, get the language type name to begin with)?

like image 228
lance Avatar asked Feb 27 '23 08:02

lance


1 Answers

You can get language specific type aliases by using CodeDom classes

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
like image 104
quentin-starin Avatar answered Mar 07 '23 20:03

quentin-starin