c# - 将类型名称作为参数用于类型名称集合的方法

标签 c# typeof system.type

当使用运算符typeof时,我们提供类型名称来获取System.Type实例:

System.Type t = typeof(System.Int32)

或者简单地说:

Type t = typeof(int)

传递给运算符的参数的确切名称/性质是什么?

如何构建此类"typename"的集合:

???[] typeNames = { System.Int32, System.String };
foreach (??? tn in typeNames) aMethod(tn);

最佳答案

What is the exact name/nature of the parameter passed to the operator?.

类型名称 - 不是字符串,而是字面上存在于代码中。

How can I build a collection of such "type names"

你不能,但你可以多次使用typeof:

Type[] types = { typeof(int), typeof(string) };

如果您确实对名称感兴趣,可以使用nameof运算符:

string[] names = { nameof(Int32), nameof(String) };

关于c# - 将类型名称作为参数用于类型名称集合的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55780738/

相关文章:

javascript - 了解 Javascript 中的 typeof 运算符

c# - 如何使用参数获取 Ctor 的 ConstructorInfo

c# - `typeof(T).IsAssignableFrom(x.GetType())` 可以安全地重写为 `x is T` 吗?

c# - 如何将 Class<Derived> 转换为 Class<Base>?

c# - 如何解决与实体依赖 DDD 中的其他实体相关的性能/内存问题?

c# - Linq 到 RSS 提要?

c# - 使用非通用 EQ-Query 从集合中删除文档

javascript - 类和数组真的只是 JavaScript 中的对象吗?

JavaScript如何计算变量的类型并返回数组的总和

c# - 检查 System.Type 是否是给定类的后代的最佳方法