4
typeof(int).Name

Will return System.Int32 does anyone know of a way to return "int"

bleevo
  • 1,637
  • 2
  • 18
  • 30

4 Answers4

7

There aren't many types that are C# keywords (int, double, string) ... so perhaps you can write your own mapping function, from the System type name to the corresonding C# keyword.

ChrisW
  • 54,973
  • 13
  • 116
  • 224
  • You can use reflection. I don't recommend doing mappings due to the number of Types out there (outside just CLR Types). There are also libraries people have written if you do want to do mappings but you don't want to implement it yourself. – Kody Jan 15 '18 at 22:27
5

Yeah, you could write a mapping function. That is just an alias anyway.

Here is a list:

http://msdn.microsoft.com/en-us/library/86792hfa(VS.71).aspx

Jay Bazuzi
  • 45,157
  • 15
  • 111
  • 168
BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
2

Using reflection, you can use CSharpCodeProvider (exists in .NET Core 2 as well) to get "int", "string", etc. instead of the full CLR Type names.

There are other SO posts that give code examples for this:

How can I get the primitive name of a type in C#?

C# - Get user-friendly name of simple types through reflection?

Alternatively, you will have to map these Types yourself. There are a few libraries to do this for you if you don't mind third party libraries. Don't forget nullable variations.

Kody
  • 905
  • 9
  • 19
0

A simple extension method can be used to map to C# keyword. Here's the code:

Ext Method for GetType return Int instead of System.Int32

JamesHoux
  • 2,999
  • 3
  • 32
  • 50