目前正在觀看IQueryable的Bart De Smet的explanation,他提到了存在類型(我一直很好奇)。閱讀答案,這樣question後,我只是想知道,如果這是一種方法來構建它在C#:此代碼是否描述了C#中的存在類型?
public abstract class SomeExistentialType
{
private SomeExistentialType() { }
public abstract int Foo();
public ExistentialType Create()
{
return new ConcreteType1();
}
private class ConcreteType1: SomeExistentialType
{
public override int Foo()
{
//some implementation...
}
}
private class ConcreteType1: SomeExistentialType
{
public override int Foo()
{
//some implementation...
}
}
private class ConcreteType1: SomeExistentialType
{
public override int Foo()
{
//some implementation...
}
}
}
的想法是,如果所有的具體類都被定義爲私有的嵌套類(或者只是內部類),那麼你只能使用接口。
我有一些人評論說,我已經在Microsoft .Connect網站上做了一個功能要求,作爲對存在類型需求的解釋。這是我第一次聽到這句話。 https://connect.microsoft.com/VisualStudio/feedback/details/576675/feature-request-for-composable-generic-types-within-a-type-definition?wa=wsignin1.0#details – jpierson 2010-12-23 02:04:50