我试图向他人展示他们创建的疯狂情况下的界面使用。它们在列表中有几个不相关的对象,并且需要对每个对象的两个字符串属性执行操作。我指出,如果他们将属性定义为接口的一部分,他们可以使用接口对象作为作用于其上的方法参数的类型;例如:有没有办法将通用列表转换为接口/基类类型列表?
void PrintProperties(IEnumerable<ISpecialProperties> list)
{
foreach (var item in list)
{
Console.WriteLine("{0} {1}", item.Prop1, item.Prop2);
}
}
这似乎是这一切都很好,但需要对工作的名单都没有(也不应该)与接口的类型参数声明。但是,它似乎并不像您可以投射到不同的类型参数。例如,这个失败,我不明白为什么:
using System;
using System.Collections.Generic;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<Test> myList = new List<Test>();
for (int i = 0; i < 5; i++)
{
myList.Add(new Test());
}
PrintList((IEnumerable<IDoSomething>)myList);
}
static void PrintList(IEnumerable<IDoSomething> list)
{
foreach (IDoSomething item in list)
{
item.DoSomething();
}
}
}
interface IDoSomething
{
void DoSomething();
}
public class Test : IDoSomething
{
public void DoSomething()
{
Console.WriteLine("Test did it!");
}
}
}
我可以使用Enumerable.Cast<T>
成员要做到这一点,但我一直在寻找可能的工作在.NET 2.0和方法。看来这应该是可能的;我错过了什么?
正是我刚写的东西。 – 2008-10-09 19:24:27
我偶然发现它,并且也要发布它。这有点奇怪,但我可以理解为什么它是这样;你必须指定类型是可施放的。 – OwenP 2008-10-09 19:25:22