How did they implement this syntax in the Massive Micro-ORM, multiple args parameters?
Asked Answered
A

2

6

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?

Acred answered 26/5, 2011 at 22:5 Comment(0)
E
8

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:

 Test(age: 40, name: "Lasse", args: new object[] { 10, 25 });
Escutcheon answered 27/5, 2011 at 12:6 Comment(2)
That makes sense, perhaps I should take a trip back to his blog and ask there... perhaps that would've been the right approach to begin with, oh well. I'll hold off on accepting this answer to see if there is a way (though I doubt it).Acred
Hm, I copied it from Rob's blog. Sorry about that.Guthrey
C
2

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)"

Coprolalia answered 30/6, 2011 at 21:23 Comment(2)
Are you sure I can call it using args:1,2,4,5 ? I can't get that to compile. I can only get this to compile: args: new[] { 1,2,4,5 }. Just to be clear, I am not asking about the args: part, I'm fully aware of how named arguments in C# 4.0 work, specifically I'm asking about the 1,2,4,5 part.Acred
Hmm - well I'm sure, and then I'm not sure. This is strangeness - if you only pass in ONE argument to args:, then it will work. If you pass in multiple, then I get the same error as you. SKEEEET!Coprolalia

© 2022 - 2024 — McMap. All rights reserved.