I need to get names of few objects. The names cannot contain their bits (16/32/64). Actaully I do it this way object.GetType().Name
. When I use int as object ( int.GetType().Name
) it returns int. But when I use uint it returns uint32. I want to get only uint. Is there a better way to do this than String.Replace?
You can use the compiler services to do so:
static string GetFriendlyTypeName<T>()
{
var csharpCodeProvider = new Microsoft.CSharp.CSharpCodeProvider();
var codeType = new System.CodeDom.CodeTypeReference(typeof(T));
return csharpCodeProvider.GetTypeOutput(codeType);
}
static void Main(string[] args)
{
Console.WriteLine(GetFriendlyTypeName<Int32>()); //int
Console.WriteLine(GetFriendlyTypeName<UInt32>()); //uint
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.