22
Type t = typeof(bool);
string typeName = t.Name;

In this simple example, typeName would have the value "Boolean". I'd like to know if/how I can get it to say "bool" instead.

Same for int/Int32, double/Double, string/String.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
  • possible duplicate of [How can I get the primitive name of a type in C#?](http://stackoverflow.com/questions/4369737/how-can-i-get-the-primitive-name-of-a-type-in-c) – Jon Skeet Jan 06 '11 at 13:52
  • 1
    Contrary to the answers given so far, look at the duplicate linked above for a more positive approach :) (Assuming you're happy to use CSharpCodeProvider rather than "plain" reflection.) – Jon Skeet Jan 06 '11 at 13:53
  • Possible duplicate of [How can I get the primitive name of a type in C#?](https://stackoverflow.com/questions/4369737/how-can-i-get-the-primitive-name-of-a-type-in-c) – Mikael Dúi Bolinder Feb 26 '19 at 17:46

7 Answers7

47
using CodeDom;
using Microsoft.CSharp;

// ...

Type t = typeof(bool);

string typeName;
using (var provider = new CSharpCodeProvider())
{
    var typeRef = new CodeTypeReference(t);
    typeName = provider.GetTypeOutput(typeRef);
}

Console.WriteLine(typeName);    // bool
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 2
    you are my new hero. I had been struggling with string manipulation and regex trying to convert the ugly type names generated for generics without and good results. This works perfectly! – Bradley Uffner Nov 24 '11 at 06:59
4

The "friendly names" as you call them are language-specific and not tied to the framework. Therefore, it doesn't make sense to have this information in the framework, and the MS design guidelines require you to use the framework names for method names etc. (such as ToInt32 etc.).

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • 3
    It is in the framework. You can get at it with CodeDom and CSharpCodeProvider. See my answer for details. – LukeH Jan 06 '11 at 14:00
  • To the downvoters - yes you can get them through CSharpCodeProvider. Because this class is C#-specific and as I said these names are language-specific, not general for the framework. Other languages on .NET do not use them or call them differently. Which is my point... – Lucero Jul 22 '21 at 20:10
3

From what I understand, bool, string, int, etc. are just aliases for us C# developers.

After the compiler processes a file, no more of them are acutally present.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
1

you can't. Those are all C# specific keywords. But you can easily map them:

switch (Type.GetTypeCode(t)) {
  case TypeCode.Byte: return "byte";
  case TypeCode.String: return "string";
}

etc

Carlo Kok
  • 1,128
  • 5
  • 14
1

The .net framework itself doesn't know about C# specific keywords. But since there are only about a dozen of them, you can simply manually create a table containing the names you want.

This could be a Dictionary<Type,string>:

private static Dictionary<Type,string> friendlyNames=new Dictionary<Type,string>();

static MyClass()//static constructor
{
  friendlyNames.Add(typeof(bool),"bool");
  ...
}

public static string GetFriendlyName(Type t)
{
  string name;
  if( friendlyNames.TryGet(t,out name))
    return name;
  else return t.Name;
}

This code doesn't replace Nullable<T> with T? and doesn't transform generics into the form C# uses.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
1

You could always create a dictionary to translate the C# names to the "friendly" ones you want:

Dictionary<System.Type, string> dict = new Dictionary<System.Type, string>();
dict[typeof(System.Boolean)] = "bool";
dict[typeof(System.string)]  = "string";
// etc...
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
0

I'd say you can't since those names are specific to C# and as such will not produce the same result if the developer wanted to use VB.NET.

You are getting the CLR type and that is really what you would want to be able to recreate the type later. But you can always write a name mapper.

jjrdk
  • 1,882
  • 15
  • 18