我試圖向他人展示他們創建的瘋狂情況下的界面使用。它們在列表中有幾個不相關的對象,並且需要對每個對象的兩個字符串屬性執行操作。我指出,如果他們將屬性定義爲接口的一部分,他們可以使用接口對象作爲作用於其上的方法參數的類型;例如:有沒有辦法將通用列表轉換爲接口/基類類型列表?
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