Frage

Is it possible in C# to accept a params argument, then pass it as params list to another function? As it stands, the function below will pass args as a single argument that is an array of type object, if I'm not mistaken. The goal here is self evident.

//ScriptEngine
public object[] Call(string fnName, params object[] args)
{
    try{
        return lua.GetFunction(fnName).Call(args);
    }
    catch (Exception ex)
    {
        Util.Log(LogManager.LogLevel.Error, "Call to Lua failed: "+ex.Message);
    }
    return null;
}

The lua.GetFunction(fnName).Call(args); is a call to outside of my code, it accepts param object[].

War es hilfreich?

Lösung

If the signature of the Call method you're calling accepts a params object[] args then you are mistaken. It doesn't consider args a single object of thpe object, to be wrapped in another array. It considers it the entire argument list, which is what you want. It'll work just fine exactly as it stands.

Andere Tipps

Yes, it is possible.In your case args actualy an array of objects.Your Call method should take params object[] or just an array of objects as parameter.

You don't need to pass more than one argument to params.You can pass an array directly.For example this is completely valid:

public void SomeMethod(params int[] args) { ... }

SomeMethod(new [] { 1, 2, 3 });

It is possible to pass the array to another function:

void Main()
{   
    int[] input = new int[] {1,2,3};
    first(input); //Prints 3
}

public void first(params int[] args)
{
    second(args);
}

public void second(params int[] args)
{
    Console.WriteLine(args.Length);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top