Does .NET have a built-in EventArgs?

前端 未结 7 1240
梦如初夏
梦如初夏 2020-11-30 06:34

I am getting ready to create a generic EventArgs class for event args that carry a single argument:

public class EventArg : EventArgs
{
    // Prope         


        
7条回答
  •  误落风尘
    2020-11-30 06:44

    THERE IS NO BUILT-IN GENERIC ARGS. If you follow Microsoft EventHandler pattern, then you implement your derived EventArgs like you suggested: public class MyStringChangedEventArgs : EventArgs { public string OldValue { get; set; } }.

    HOWEVER - if your team style guide accepts a simplification - your project can use a lightweight events, like this:

    public event Action MyStringChanged;
    

    usage :

    // How to rise
    private void OnMyStringChanged(string e)
    {
        Action handler = MyStringChanged;    // thread safeness
        if (handler != null)
        {
            handler(this, e);
        }
    }
    
    // How to handle
    myObject.MyStringChanged += (sender, e) => Console.WriteLine(e);
    

    Usually a PoC projects use the latter approach. In professional applicatons, however, be aware of FX cop justification #CA1009: https://msdn.microsoft.com/en-us/library/ms182133.aspx

提交回复
热议问题