一个ArrayList和阵列之间的运营商 - 不能申请? [英] Cannot apply ??-operator between an ArrayList and an Array
问题描述
我有code是这样的:
I have code like this:
foreach (Type t in types ?? asm.GetTypes())
这应该回路中的的ArrayList
类型
或组件类型的所有类型,如果没有提供类型。由于两个的System.Array
为 System.Collections.ArrayList
实施的IEnumerable
我倒是想到的是循环工作。
Which should loop all types in the ArrayList
types
or the assemblies types if no types are provided. As both System.Array
as System.Collections.ArrayList
implement IEnumerable
I´d expect that loop to work.
但是编译器抱怨:
操作'?不能应用于类型'System.Collections.ArrayList'和'的System.Type []'
Operator '??' cannot be applied to operands of type 'System.Collections.ArrayList' and 'System.Type[]'
我想我失去了一些东西很明显,但它是什么?
I suppose I am missing something very obvious, but what is it?
推荐答案
您真的不应该使用的ArrayList
在2015年的ArrayList
不支持的IEnumerable<方式>
接口
You shouldn't really use ArrayList
in 2015. ArrayList
doesn't support the IEnumerable<>
interface.
您可以强制的IEnumerable
非泛型接口的使用:
You can force the using of the IEnumerable
non-generic interface:
foreach (Type t in (IEnumerable)types ?? asm.GetTypes())
您错误发生,因为 ??
运营商不搜索左右之间的最小常见的类型。它只是检查是否正确的类型可以隐式转换到左的类型,或者离开类型可以隐式转换为正确的类型。显然键入[]
不能转换为的ArrayList
和的ArrayList
不能转换为键入[]
。
Your error happens because the ??
operator doesn't search for the minimum common type between left and right. It simply checks if right type can be implicitly converted to left type or if left type can be implicitly converted to right type. Clearly Type[]
can't be converted to ArrayList
and ArrayList
can't be converted to Type[]
.
这篇关于一个ArrayList和阵列之间的运营商 - 不能申请?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!