How did they implement this syntax in a Micro-ORM array, several args parameters?

Here on this page, Scott Hanselman shows two examples from Micro-ORMs Dapper and Massive, and the Massive example drew me to this because I don’t see how they can implement this syntax.

An example is the following, where I am going to split it into several lines instead of 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 have multiple values? I accept params arguments because this is the only thing that allows it, but I don’t understand how they built the method to allow this, because it seems to me that everything I try ends up complaining about names, arguments and fixed position arguments are in wrong order.

I tried a test method:

 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:

Named arguments must appear after all fixed arguments have been defined.

so it’s obvious that it’s wrong. In addition, I do not see anything in the source that would allow this, but maybe I'm looking in the wrong place.

What am I missing here?

+6
source share
2 answers

In fact, I think that Mr. Hanselman showed some kind of code that does not compile (unfortunately, did I really dare to say this?). I can get it to work as follows:

  Test(age: 40, name: "Lasse", args: new object[] { 10, 25 }); 
+8
source

These are the just named arguments in C # 4.0. You can specify your arguments using the parameter name, as you see in the above call.

To accept an array (as you can see with several "args") - you simply use the keyword "params":

public void MyMethod (line arg1, params object [] args) {// ..}

Now, to call this method in C # 4.0, you can use "MyMethod (arg1:" Lasse ", args: 1,2,4,5)"

+2
source

Source: https://habr.com/ru/post/889158/


All Articles