目前正在观看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