以类型名称作为参数的方法,用于类型名称的集合
Method taking a type name as argument used against a collection of type names
当使用运算符 typeof
时,我们提供一个类型名称来获取 System.Type
实例:
System.Type t = typeof(System.Int32)
或者简单地说:
Type t = typeof(int)
传递给运算符的参数的确切 name/nature 是多少?
如何构建这样的集合 "type names":
???[] 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) };
如果您确实对 names 感兴趣,可以使用 nameof
运算符:
string[] names = { nameof(Int32), nameof(String) };
当使用运算符 typeof
时,我们提供一个类型名称来获取 System.Type
实例:
System.Type t = typeof(System.Int32)
或者简单地说:
Type t = typeof(int)
传递给运算符的参数的确切 name/nature 是多少?
如何构建这样的集合 "type names":
???[] 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) };
如果您确实对 names 感兴趣,可以使用 nameof
运算符:
string[] names = { nameof(Int32), nameof(String) };