开发者

Pass additional parameters or objects using an Event Handler [duplicate]

开发者 https://www.devze.com 2023-04-13 07:50 出处:网络
This question already has answers here: Pass extra parameters to an event handler? (10 answers) Closed 8 years ago.
This question already has answers here: Pass extra parameters to an event handler? (10 answers) Closed 8 years ago.

I feel like this is really basic, but I'm having trouble with this issue. I'm using a Process object and subscribing to a DataReceivedEventHandler. This event handler then delegates to another method, in this case "DoSomething", and the signature for the arguments is (Object sender, DataReceivedEventArgs args). What I need to do, is extend something, or provide something that will pass in additional information. Here is my current code:

// an object of some type
MyCustomObject obj = new MyCustomObject();

// set up obj and Process

process.OutputDataReceived += new DataReceivedEventHandler(DoSomething);

public void DoSomething(Object sender, DataReceivedArgs args)
{
    // do some stuff, however, I need the "obj" object passed in for work
}

I feel like this is really trivial, but not sure how to proceed. I've read about subclassing the "EventArgs," but not sure how that will help, or how to even change the signature of "DoSomething" to accept a DataReceivedArgsExtended parameter, s开发者_StackOverflow社区ince the DataReceivedEventHandler is expecting a method with a DataReceivedArgs


Yes you can extend your DataReceivedArgs to DataReceivedArgsExtended, but remeber cast it into event handler method. For example:

public class MyObject
{
    public event EventHandler<MyEventArgs> OnFire;

    public void Fire()
    {
        if( OnFire != null )
        {
            //var e = new MyEventArgs { X=2 };

            var e = new MyEventArgsNew { X = 3, Y = 4 };

            OnFire( this, e );
        }
    }
}

public class MyEventArgs : EventArgs
{
    public int X { get; set; }
}

public class MyEventArgsNew : MyEventArgs
{
    public int Y { get; set; }
}

static void Main( string[] args )
{
        var obj = new MyObject();

        obj.OnFire += new EventHandler<MyEventArgs>( obj_OnFire );

        obj.Fire();

        Console.ReadLine();
 }

 static void obj_OnFire( object sender, MyEventArgs e )
 {
        var e2 = (MyEventArgsNew)e;

        Console.WriteLine( e2.X );
        Console.WriteLine( e2.Y );
 }


Your question is already answered in the following questions:

C# passing extra parameters to an event handler?

How can I pass addition local object variable to my event handler? [duplicate]

In your case you need to use an anonymous delegate function or a lambda expression in the same scope and from within it call your own function, containing the event handle parameters and your additional ones:

// an object of some type
MyCustomObject obj = new MyCustomObject();

// set up obj and Process

process.OutputDataReceived +=
    (Object _sender, DataReceivedArgs _args) =>
        DoSomething(obj, _sender, _args);

public void DoSomething(Process process, Object sender, DataReceivedArgs args)
{
    // do some stuff
}
0

精彩评论

暂无评论...
验证码 换一张
取 消