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.
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
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.).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With