C# type name instead of CLR type name

限于喜欢 提交于 2019-12-19 09:28:09

问题


typeof(int).Name

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


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/408659/c-sharp-type-name-instead-of-clr-type-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!