具有继承性的可扩展Fluent接口 [英] Scaleable Fluent Interface with Inheritance
问题描述
我正在尝试编写一个可以很好地扩展的Fluent Interface API。什么结构允许强类型,继承和状态满(如在类类型中)?
I am trying to write a Fluent Interface API that can scale well. What structure would allow for strong types, inheritance, and state-full(as in the class type)?
例如
class A
{
public A PerformFoo()
{
//do stuff
return this;
}
}
class B : A
{
}
我希望B级在致电PerformFoo时返回B而不是A,理想情况下我宁愿远离
I would like class B when calling PerformFoo to return B not A, ideally I would prefer to stay away from
public class B : A
{
public new B PerformFoo()
{
return (B)base.PerformFoo();
}
}
至于不必覆盖或新的每个方法儿童班。我相信我需要使用使用类型签名的泛型。
As to not have to override or new Every method in child classes. I believe I would need to use generics that use Type signatures.
我不想使用扩展方法但似乎无法在执行时获得正确的结构与[链接]的答案中的铸造(T)一样流畅的接口和继承在C#
I don't want to use extension methods but can't seem to get the structure right when doing it with casting (T) like in the answer for [a link]Fluent interfaces and inheritance in C#
推荐答案
最后我想出了结构
public class A {}
public class B : A {}
public class C<T> : A where T : C<T>
{/*most methods return T*/}
public class D:C<D>
{/*all methods return this*/}
public class E<T>:C<T> where T:E<T>
{/*all methods return T*/}
public class F:E<F>{}
现在即使是专门的泛型也会返回原来的来电者
now even specialized generics will still return the original caller
这篇关于具有继承性的可扩展Fluent接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!