他们是如何在 Massive Micro-ORM、多个 args 参数中实现这个语法的?
在此页面上,Scott Hanselman 展示了来自 Micro-ORM Dapper 和 Massive 的两个示例,以及Massive-example 引起了我的兴趣,因为我不知道他们如何实现该语法。
示例如下,我将把它分成几行,而不是一长行:
var tbl = new Products();
var products = tbl.All(where: "CategoryID = @0 AND UnitPrice > @1",
orderBy: "ProductName", limit: 20, args: 5,20);
^----+---^
|
+-- this
他们是如何实现这种语法的,允许 args
有多个值?我假设基于 params
的参数,因为这是唯一允许这样做的东西,但我不明白他们如何构建允许这种情况的方法,因为在我看来,我所做的一切最终抱怨命名参数和固定位置参数的顺序错误。
我尝试了这样的测试方法:
public static void Test(string name, int age, params object[] args)
{
}
然后使用命名参数:
Test(age: 40, name: "Lasse", args: 10, 25);
但我得到的只是:
命名参数规范必须出现在指定所有固定参数之后
因此显然这是错误的。另外,我在源代码中看不到任何允许这样做的内容,但也许我找错了地方。
我在这里缺少什么?
Here on this page, Scott Hanselman shows two examples from Micro-ORMs Dapper and Massive, and the Massive-example caught my interested, because I don't see how they could implement that syntax.
The example is as follows, where I'm going to break it over several lines instead of just one long one:
var tbl = new Products();
var products = tbl.All(where: "CategoryID = @0 AND UnitPrice > @1",
orderBy: "ProductName", limit: 20, args: 5,20);
^----+---^
|
+-- this
How did they implement this syntax, allowing args
to have multiple values? I'm assuming params
-based arguments, because that's the only thing that allows for that, but I don't understand how they constructed the method to allow for that since it seems to me that all I try ends up complaining about named arguments and fixed position arguments are in the wrong order.
I tried a test-method like this:
public static void Test(string name, int age, params object[] args)
{
}
and then using named arguments:
Test(age: 40, name: "Lasse", args: 10, 25);
But all I get is this:
Named argument specifications must appear after all fixed arguments have been specified
so obviously that is wrong. Also I can't see in the source anything that would allow for this but maybe I'm looking in the wrong place.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上我认为 Hanselman 先生展示了一些无法编译的代码(哎呀,我真的敢这么说吗?)。 我只能让它像这样工作:
Actually I think that Mr. Hanselman showed some code that doesn't compile (oops, did I really dare to say that?). I can only get it working like this:
这只是 C# 4.0 中的命名参数。您可以使用上面调用中看到的参数名称来指定参数。
要接受数组(如您在多个“args”中看到的那样) - 您只需使用“params”关键字:
public void MyMethod(string arg1, params object[] args){
//..
现在
,要在 C# 4.0 中调用此方法,您可以使用 "MyMethod(arg1: "Lasse", args:1,2,4,5)"
This is just named arguments in C# 4.0. You can specify your arguments by using the name of the parameter as you see in your call above.
To accept an array (as you see with the multiple "args") - you simply use the "params" keyword:
public void MyMethod(string arg1, params object[] args){
//..
}
Now, to call this method in C# 4.0, you could use "MyMethod(arg1: "Lasse", args:1,2,4,5)"