I’m trying to extend DataReceivedEventArgs so that I can pass in additional data to a class that is extending Process. Pretty much rather than just get Data from a process when hooking up to Process.OutputDataReceived, I would like to pass in a control for it to write to.
When trying to extend DataReceivedEventArgs I get errors:
The type 'System.Diagnostics.DataReceivedEventArgs' has no constructors defined
public class DataReceivedArgsWithControl : DataReceivedEventArgs
{
public Control ControlAdded { get; set; }
}
How can I add another property to this Args? I’ve extended EventArgs itself because it has a constructor, but not sure how to extend this Args.
I suspect that you can’t because the constructor is
Internal. Perhaps a better approach would be to wrap theDataReceivedEventArgsinside yourEventArgsderived class.Of course, this might not be suitable if you need the polymorphism with
DataReceivedEventArgs. If you have an event handler that is expecting aDataReceivedEventArgsthen it won’t work with the wrapper class. For example:This could only receive a
DataReceivedEventArgsinstance or an instance of a derived type, which your wrapper is not. So it depends if you need to treat your custom EventArgs class is if it were aDataReceivedEventArgsanywhere.UPDATE-
If you can’t change the signature of the delegate you’re using from
public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e)then you can still subscribe using a method with the signaturevoid MyEventHandler(object sender, EventArgs e)thanks to contravariance of delegate parameters and then check the actual type of theEventArgsparameter.The ideal option would be to redefine the delegate type to match your wrapper, but the above approach will work.