关于C#中Fluent接口的问题
我有以下课程:
public class Fluently
{
public Fluently Is(string lhs)
{
return this;
}
public Fluently Does(string lhs)
{
return this;
}
public Fluently EqualTo(string rhs)
{
return this;
}
public Fluently LessThan(string rhs)
{
return this;
}
public Fluently GreaterThan(string rhs)
{
return this;
}
}
在英语语法中,你不能有“某物等于某物”或“某物大于某物”,所以我不希望 Is.EqualTo 和Does.GreaterThan 成为可能。有什么办法限制吗?
var f = new Fluently();
f.Is("a").GreaterThan("b");
f.Is("a").EqualTo("b"); //grammatically incorrect in English
f.Does("a").GreaterThan("b");
f.Does("a").EqualTo("b"); //grammatically incorrect in English
谢谢你!
I have the following class:
public class Fluently
{
public Fluently Is(string lhs)
{
return this;
}
public Fluently Does(string lhs)
{
return this;
}
public Fluently EqualTo(string rhs)
{
return this;
}
public Fluently LessThan(string rhs)
{
return this;
}
public Fluently GreaterThan(string rhs)
{
return this;
}
}
In English grammar you can’t have “is something equal to something” or “does something greater than something” so I don’t want Is.EqualTo and Does.GreaterThan to be possible. Is there any way to restrict it?
var f = new Fluently();
f.Is("a").GreaterThan("b");
f.Is("a").EqualTo("b"); //grammatically incorrect in English
f.Does("a").GreaterThan("b");
f.Does("a").EqualTo("b"); //grammatically incorrect in English
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了强制执行这种类型的事情,您需要多种类型(以限制从哪个上下文中可用的内容)-或者至少需要一些接口:
To enforce that type of thing, you'll need multiple types (to restrict what is available from which context) - or at the least a few interfaces:
在我看来,我的解决方案
与 Gravell 的解决方案非常相似,但更容易理解。
My solution would be
Quite similar to Gravell's, but slightly simpler to understand, in my opinion.