简体   繁体   English

C# - 将扩展的参数数组传递给另一个 function

[英]C# - pass on expanded params array to another function

How can I expand a params array when passing it on to another function.将参数数组传递给另一个 function 时如何扩展params数组。

Consider the following example:考虑以下示例:

public class TestClass
{
    public static void Main()
    {
        TestParamFunc(1, "1", 1.0f, 1.0);

        Console.ReadKey();
    }

    private static void TestParamFunc(params object[] parameters)
    {
        //At the moment the DeeperTestParamFunc receives the following parameters: { int, object[] }
        //How to expand this to { int, int, string, float, double } ?
        //So that { 1, 1, "1", 1.0f, 1.0 } is received.
        DeeperTestParamFunc(1, parameters);
    }

    public static void DeeperTestParamFunc(params object[] parameters)
    {
        foreach(object obj in parameters)
        {
            Console.WriteLine(obj.GetType().ToString());
        }
    }
}

Output: Output:

System.Int32
System.Object[]

The output that I would like to have:我想拥有的 output:

System.Int32
System.Int32
System.String
System.Single
System.Double

You make a new array, copy the existing parameters into it, add more things and call the other method with the larger array您创建一个新数组,将现有参数复制到其中,添加更多内容并使用更大的数组调用另一个方法

These are, when all is said and done, just arrays.说到底,这些就是 arrays。 params just means the compiler lets you specify arguments individually and it will turn it into an array for you params只是意味着编译器允许您单独指定 arguments ,它将为您将其转换为数组

You write this:你这样写:

MyFuncWithParamsObjectArg(1,2,3,4,5);

The compiler conceptually changes it to:编译器在概念上将其更改为:

MyFuncWithParamsObjectArg(new object [] {1,2,3,4,5} );

Here's a demo program:这是一个演示程序:

public static void Main()
{
    A("z", "y");
    
}

static void A(params object[] a){
    Console.WriteLine("In A()");
    
    foreach(object o in a)
        Console.WriteLine(o);
    
    object[] oo = new object[a.Length + 2];
    
    for(int i = 0; i < a.Length; i++)
        oo[i] = a[i];
    
    oo[a.Length] = "new";
    oo[a.Length + 1] = "new2";
    
    B(oo);
}

static void B(params object[] b){
    Console.WriteLine("In B()");
    
    foreach(object o in b)
        Console.WriteLine(o);
}

Prints out this:打印出来:

In A()
z
y
In B()
z
y
new
new2

You can see that B was called with an array 2 longer than a, and added two more elements你可以看到 B 被调用了一个比 a 长 2 的数组,并添加了两个元素

Note, you can't "expand" an array, regardless of whether it's params or not.注意,你不能“扩展”一个数组,不管它是否是参数。 You have to make a new array, copy the stuff over, and go from there您必须创建一个新数组,将内容复制过来,然后从那里 go

If you're looking to wrap it up so that you're not flattening the first array, but instead making another params array that has the first array as one of its elements:如果您希望将其包装起来,这样您就不会展平第一个数组,而是制作另一个将第一个数组作为其元素之一的 params 数组:

static void A(params object[] a){
    Console.WriteLine("In A()");
    
    foreach(object o in a)
        Console.WriteLine(o);
    
    B(a, "new1", "new2");
}

B will now get an array that looks like: B 现在将得到一个如下所示的数组:

object[] {
   object[] { "z", "y" },
   "new1",
   "new2"
}

As noted;如前所述; there is no magic - the compiler looks at what you provided and if the arguments match "called with a single dimension array" then it calls the function with the array, otherwise it scoops up the various arguments, turns them into an array, and calls it with that array没有魔法 - 编译器会查看您提供的内容,如果 arguments 匹配“使用单维数组调用”,那么它会使用数组调用 function,否则它会挖出各种 ZDBC11CAA5BDA99F77E6FB4DABD8882E7它与那个数组

B(a);                                  //a is already an object array, it is passed in verbatim
B(a, "new1", "new2");                  //a new object array is created from these 3 objects
B(new object[] {a, "new1", "new2"} );  //manually doing what the compiler does for you above

this is what you want:这就是你想要的:

    private static void TestParamFunc(params object[] parameters)
    {
        // manually create the array in the form you need
        var deeperParameters = new object[parameters.Length + 1];
        deeperParameters[0] = 1;
        parameters.CopyTo(deeperParameters, 1);            
        DeeperTestParamFunc(deeperParameters);
    }

try:尝试:

public static void DeeperTestParamFunc(params object[] parameters)
        {
            foreach (object obj in (IEnumerable) parameters[1])
            {
                Console.WriteLine(obj.GetType().ToString());
            }
        }

instance:实例:

public static void DeeperTestParamFunc(params object[] parameters)
    {
        foreach(object obj in parameters)
        {
            Console.WriteLine(obj.GetType().ToString());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM