当使用运算符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/