I would like to know how to pass object array (object[]) between classes with the code bellow, which now sends string only:

public delegate void MessageDelegate(Object sender, MessageEventArgs e);

  public class MessageEventArgs : EventArgs
  {
    public string Message;
    public MessageEventArgs(string msg)
    {
      this.Message = msg;
    }
  }
  public interface IController
  {
    event MessageDelegate ControllerEvent;
    void InvokeControllerEvent(string msg);
  }
  public class Controller : IController
  {
    public event MessageDelegate ControllerEvent;

    public void InvokeControllerEvent(string msg)
    {
      this.OnControllerEvent(new MessageEventArgs(msg));
    }

    protected virtual void OnControllerEvent(MessageEventArgs mea)
    {
      if (this.ControllerEvent != null)
      {
        this.ControllerEvent.Invoke(this, mea);
      }
    }
  }

Dani AI

Generated

— short answer: carry the array inside the EventArgs (or a read-only wrapper) and raise the event with that EventArgs. ’s sample proves passing an array as a parameter works; ’s suggestion to put an object[] on your MessageEventArgs is the simplest fix. Below is a slightly safer, more idiomatic pattern you can apply.

public sealed class PayloadEventArgs : EventArgs
{
    public object[] Payload { get; }

    public PayloadEventArgs(object[] payload)
    {
        Payload = payload == null ? Array.Empty<object>() : (object[])payload.Clone();
    }
}
public interface IController
{
    event EventHandler<PayloadEventArgs> ControllerEvent;
    void Publish(params object[] payload);
}

public class Controller : IController
{
    public event EventHandler<PayloadEventArgs> ControllerEvent;

    public void Publish(params object[] payload)
    {
        var args = new PayloadEventArgs(payload);
        ControllerEvent?.Invoke(this, args);
    }
}

Notes and cautions:

  • Clone the array (shown above) to avoid callers mutating the payload after you raise the event. Arrays are reference types.
  • Expose a read-only view (IReadOnlyList<object> or ReadOnlyCollection<object>) if consumers must not be able to modify contents.
  • If the payload has a known type, prefer a generic PayloadEventArgs<T> to avoid boxing/unboxing and to get compile-time safety.
  • Prefer EventHandler<T> and a Publish/Send method (as shown); avoid exposing a public method on the interface whose sole purpose is “raise the event” unless that fits your design, since normally the owner raises its own events.
  • Use the null-conditional invoke (ControllerEvent?.Invoke(...)) or copy-to-local for thread-safety when raising events.

Recommended Answers

All 2 Replies

I would like to know how to pass object array (object[]) between classes with the code bellow, which now sends string only:

static void Sample(object[] ar)
        {
            if(ar is string[])
                Console.WriteLine( ((string[])ar)[0]);
        }
        static void Sample1(Array ar)
        {
            if (ar is string[])
                Console.WriteLine(((string[])ar)[0]);
        }
        static void Main(string[] args)
        {
            string[] ar = { "AA", "BB" };
            Sample(ar);
            Sample1(ar);
        }

As long as it is within you own app and not a remote app or other process you can just add an object[] as an additional parameter to your MessageEventArgs .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.