如何将对象数组作为单个参数传递给方法? [英] How do I pass an object array into a method as individual parameters?

查看:130
本文介绍了如何将对象数组作为单个参数传递给方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

public class MyClass
{
    private Delegate m_action;
    public object[] m_args;

    public MyClass()
    {

    }

    public MyClass(Delegate action, params object[] args)
    {
        m_args = args;
        m_action = action;
    }

    public void Execute()
    {
        m_action.DynamicInvoke(m_args);
    }
}

这种方法的问题在于m_args本身就是一个对象,并且其内容没有被展平到各个params条目中.我该如何解决?

The problem with this approach is that the m_args is an object itself, and its contents are not being flattened out into individual params entries. How can I fix this?

推荐答案

我认为您错了. params似乎按预期工作.这是一个更简单的示例,说明它的工作原理:

I think you are mistaken. The params seems to work as intended. Here's a simpler example showing that it works:

static void f(params object[] x)
{
    Console.WriteLine(x.Length);
}

public static void Main()
{
    object[] x = { 1, 2 };
    f(x);
}

结果:

2

查看其在线运行情况: ideone

See it working online: ideone

这篇关于如何将对象数组作为单个参数传递给方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆