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

本文是小编为大家收集整理的关于如何将一个对象数组作为单独的参数传入方法?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我有以下代码:

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似乎按预期工作.这是一个更简单的示例,显示它有效:

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

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

结果:

2

请参阅在线工作: ideone

其他推荐答案

这就像您认为会一样.

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 );
  }
}

class Program
{
  static void Main( string[] args )
  {
    Type       tSelf          = typeof(Program) ;
    MethodInfo mi             = tSelf.GetMethod( "FooBar" ) ;
    Delegate   methodDelegate = Delegate.CreateDelegate( typeof(Action<int,int,int>) , mi ) ;
    MyClass    instance      = new MyClass( methodDelegate , 101 , 102 , 103 ) ;

    instance.Execute() ;

    return;
  }

  public static void FooBar( int x , int y , int z )
  {
    Console.WriteLine( "x:{0}, y:{1}, z:{2}" , x , y , z ) ;
    return ;
  }

}

本文地址:https://www.itbaoku.cn/post/1730668.html

问题描述

I have the following code:

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);
    }
}

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?

推荐答案

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);
}

Result:

2

See it working online: ideone

其他推荐答案

This works just like you'd think it would.

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 );
  }
}

class Program
{
  static void Main( string[] args )
  {
    Type       tSelf          = typeof(Program) ;
    MethodInfo mi             = tSelf.GetMethod( "FooBar" ) ;
    Delegate   methodDelegate = Delegate.CreateDelegate( typeof(Action<int,int,int>) , mi ) ;
    MyClass    instance      = new MyClass( methodDelegate , 101 , 102 , 103 ) ;

    instance.Execute() ;

    return;
  }

  public static void FooBar( int x , int y , int z )
  {
    Console.WriteLine( "x:{0}, y:{1}, z:{2}" , x , y , z ) ;
    return ;
  }

}