使用object []和object [] []作为参数的C#调用方法(params object [] [])

时间:2022-01-07 03:07:12

I have a method

我有一个方法

public void method(params object[][] arg)

But where I call the method, I have and object[] and object[][]:

但是在我调用方法的地方,我有对象[]和对象[] []:

object[] var1 = new object[5];
object[][] var2 = new object[8][5];
method(var1, var2); (I´d like this to be method(var1, var2[0], var2[1], var2[2],...)

But inside method, arg has length 2:

但是在内部方法中,arg的长度为2:

arg[0] = var1
arg[1] = var2

But I want arg as an array of length 9:

但我希望arg作为长度为9的数组:

arg[0] = var1
arg[1] = var2[0];
arg[2] = var2[1];
... 

How can I call the method?

我怎么称呼这个方法?

2 个解决方案

#1


3  

It sounds like you want:

这听起来像你想要的:

object[][] args = new[] { var1 }.Concat(var2).ToArray();
method(args);

The "normal" parameter array handling of C# doesn't do any flattening: you either call it with a single argument which is already the right type, or you call it with a sequence of arguments of the element type. You can't mix and match.

C#的“普通”参数数组处理不会进行任何展平:您可以使用单个参数调用它,该参数已经是正确的类型,或者使用元素类型的参数序列调用它。你不能混搭。

#2


1  

In general, for methods that accept a params array I also provide an overload that accepts IEnumerable<T>:

通常,对于接受params数组的方法,我还提供了一个接受IEnumerable 的重载:

public void MyMethod(params string[] args)
{
    MyMethod((IEnumerable<string>)args);
}

public void MyMethod(IEnumerable<string> args)
{
    // ...
}

Then you can also easily provide an enumerable as the parameter. For example:

然后,您还可以轻松提供可枚举的参数。例如:

string[] var1 = new string[5];
string[][] var2 = new string[8][5];

// Jon Skeet's suggestion without ToArray():
var sequence = new[] { var1 }.Concat(var2);
MyMethod(sequence);

If you really want to use the syntax you propose, you can do that since in your example you use object[]. An array of objects is also an object, therefore you can do the flattening inside your method:

如果你真的想使用你提出的语法,你可以这样做,因为在你的例子中你使用object []。对象数组也是一个对象,因此您可以在方法内部进行展平:

public void MyMethod(params object[] args)
{
    object[] flattened = arg
        // For each element that is not in an array, put it in an array.
        .Select(a => a is object[] ? (object[])a : new object[] { a })
        // Select each element from each array.
        .SelectMany(a => a)
        // Make the resulting sequence into an array.
        .ToArray();
    // ...
}

MyMethod(var1, var2);

#1


3  

It sounds like you want:

这听起来像你想要的:

object[][] args = new[] { var1 }.Concat(var2).ToArray();
method(args);

The "normal" parameter array handling of C# doesn't do any flattening: you either call it with a single argument which is already the right type, or you call it with a sequence of arguments of the element type. You can't mix and match.

C#的“普通”参数数组处理不会进行任何展平:您可以使用单个参数调用它,该参数已经是正确的类型,或者使用元素类型的参数序列调用它。你不能混搭。

#2


1  

In general, for methods that accept a params array I also provide an overload that accepts IEnumerable<T>:

通常,对于接受params数组的方法,我还提供了一个接受IEnumerable 的重载:

public void MyMethod(params string[] args)
{
    MyMethod((IEnumerable<string>)args);
}

public void MyMethod(IEnumerable<string> args)
{
    // ...
}

Then you can also easily provide an enumerable as the parameter. For example:

然后,您还可以轻松提供可枚举的参数。例如:

string[] var1 = new string[5];
string[][] var2 = new string[8][5];

// Jon Skeet's suggestion without ToArray():
var sequence = new[] { var1 }.Concat(var2);
MyMethod(sequence);

If you really want to use the syntax you propose, you can do that since in your example you use object[]. An array of objects is also an object, therefore you can do the flattening inside your method:

如果你真的想使用你提出的语法,你可以这样做,因为在你的例子中你使用object []。对象数组也是一个对象,因此您可以在方法内部进行展平:

public void MyMethod(params object[] args)
{
    object[] flattened = arg
        // For each element that is not in an array, put it in an array.
        .Select(a => a is object[] ? (object[])a : new object[] { a })
        // Select each element from each array.
        .SelectMany(a => a)
        // Make the resulting sequence into an array.
        .ToArray();
    // ...
}

MyMethod(var1, var2);