我从 Built-in types table (C# Reference) 获得了所有内置类型. 有没有办法以编程方式获取此列表? 我不知道如何使用反射,所以我什至不知道如何开始。我还没有代码。
最佳答案
It's not a good idea to get those types using reflection. Just for learning purpose you use the following snippets.
获取框架基元类型全名:
var frameworkTypesFullName = typeof(Type).Assembly.GetTypes()
.Where(x => x.IsPrimitive).Select(x => x.FullName).ToList();
获取原始类型的 C# 别名:
var cs = new CSharpCodeProvider(); //dispose later or put in using statement
var csharpTypesAlias = typeof(Type).Assembly.GetTypes()
.Where(x => x.IsPrimitive).Select(x =>
cs.GetTypeOutput(new CodeTypeReference(x))).ToList();
还有 System.String
和 System.Object
不是原始类型,但通常命名为 Simple Types with string
和 object
C# 中的别名。
关于c# - 以编程方式获取所有内置类型的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59005990/