创建流畅的 api 时,链接方法如何从以前的方法获取上下文?
我正在查看这个开源项目,流畅的 api 如下所示:
baseEngine.For<Foo1>()
.Setup(f => f.Value)
.MustEqual(1);
然后 MustEqual 方法的参数列出它:
public static M MustEqual<M, T, R>(this IMustPassRule<M, T, R> mpr, R value)
{
return mpr.MustPassRule(new EqualRule<R>(value));
}
有关更多详细信息: http://rulesengine.codeplex.com/SourceControl/changeset/view/9077#137411
那又怎样我试图了解的是,对 MustEqual 的调用仅传递了一个参数,因为它是流畅的,它是否以某种方式隐式地从先前链接的调用中获取其他所需的参数?
I'm looking at this open source project, and the fluent api looks like:
baseEngine.For<Foo1>()
.Setup(f => f.Value)
.MustEqual(1);
Then MustEqual method's parameter list it:
public static M MustEqual<M, T, R>(this IMustPassRule<M, T, R> mpr, R value)
{
return mpr.MustPassRule(new EqualRule<R>(value));
}
for more details: http://rulesengine.codeplex.com/SourceControl/changeset/view/9077#137411
So what I'm trying to get at is, the call to MustEqual is being passed only a single arguement, since it is fluent, is it somehow implicitly picking up other required parameters from the previously chained calls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LINQ 中可以找到一个很好的示例
IEnumerable.OrderBy
返回一个IOrderedEnumerable
第二个接口跟踪订单,因此
IOrderedEnumerable< T>.ThenBy
能够进行子排序。为了使事情顺利,该界面派生自IEnumerable
A nice example can be found in LINQ
The
IEnumerable<T>.OrderBy
returns anIOrderedEnumerable<T>
This second interface keeps track of the order so the
IOrderedEnumerable<T>.ThenBy
is able to do a sub-sort. To make things smooth the interface derives fromIEnumerable<T>
如有必要,状态会通过之前的方法存储在对象本身中。
The state gets stored in the object itself by those previous methods, if necessary.
您正在查看扩展方法。
扩展方法允许使用实例方法语法调用静态方法。例如,
相当于
You are looking at an Extension Method.
Extension Methods allow static methods to be invoked with instance method syntax. For instance,
is equivalent to