class A : IFoo
{
}
...
A[] arrayOfA = new A[10];
if(arrayOfA is IFoo[])
{
// this is not called
}
Q1的阵列:为什么arrayOfA
不是IFoos
数组?铸造对象(实现接口的IFoo)到的IFoo []在C#
Q2:为什么我不能投arrayOfA
到IFoo[]
?
class A : IFoo
{
}
...
A[] arrayOfA = new A[10];
if(arrayOfA is IFoo[])
{
// this is not called
}
Q1的阵列:为什么arrayOfA
不是IFoos
数组?铸造对象(实现接口的IFoo)到的IFoo []在C#
Q2:为什么我不能投arrayOfA
到IFoo[]
?
arrayOfA
是IFoo[]
。
您的程序一定有其他问题。
你似乎已经嘲笑了一些代码来说明问题,但实际上你的代码(见下文)的作品如您所愿。尝试用真实代码更新此问题 - 或者尽可能接近真实 - 然后我们可以再看一次。
using System;
public class oink {
public static void Main() {
A[] aOa = new A[10];
if (aOa is IFoo[]) { Console.WriteLine("aOa is IFoo[]"); }
}
public interface IFoo {}
public class A : IFoo {}
}
PS D:\> csc test.cs
Microsoft (R) Visual C# 2008 Compiler version 3.5.30729.1
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.
PS D:\> D:\test.exe
aOa is IFoo[]
PS D:\>
你可以尝试
if (arrayofA[0] is IFoo) {.....}
哪种类型的回答你的问题。 arrayOfA
是一个数组。数组是一个对象,它实现了ICloneable
,IList
,ICollection
,& IEnumerable
。其中不包括IFoo
。