.NET Events (C #) - Custom EventArgs Question

I have some special C # events related to items selected in a tree, and then a context menu used to trigger some actions on selected items. I started creating a separate EventArgs-based class for each custom event to pack the necessary data for the event.

However, I understand that most (possibly all) of my user events will need to transmit at least a list of basic objects represented by a tree selection. Some events will also need additional data.

Given this, I wonder if one of the following rules is acceptable?

  • Use the same EventArgs-based native class for multiple events (those who just need a list of objects to pass in). Obviously this should work, but it seems to break away from some of the recommended naming conventions used to connect event equipment.

  • Create a base class that completes my often needed list of objects, and then derives additional classes from it, since additional data is needed.

  • Maybe something else?

Currently, I have only a few special events, but you need to add more. Since I see a pattern arising regarding the data needed for each event, I would like to have a better plan before continuing.

Thanks for any advice.

+5
4

. , . , , . . , .

+5

.NET Framework . - . :

  • System.ComponentModel.ProgressChangedEventArgs ( ProgressPercentage UserState )
    • System.Net.DownloadProgressChangedEventArgs
    • System.Net.UploadProgressChangedEventArgs
    • System.Windows.Documents.Serialization.WritingProgressChangedEventArgs
  • System.ComponentModel.CancelEventArgs ( Cancel )
    • Microsoft.VisualBasic.ApplicationServices.StartupEventArgs
    • System.ComponentModel.DoWorkEventArgs
    • System.Configuration.SettingChangingEventArgs
    • System.Drawing.Printing.PrintEventArgs
    • System.Web.UI.WebControls.DetailsViewDeleteEventArgs
    • System.Web.UI.WebControls.DetailsViewInsertEventArgs
    • System.Web.UI.WebControls.DetailsViewModeEventArgs
    • System.Web.UI.WebControls.DetailsViewPageEventArgs
    • System.Web.UI.WebControls.DetailsViewUpdateEventArgs
    • System.Web.UI.WebControls.EntityDataSourceChangingEventArgs
    • 50

:

( ).

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

public class CollectionEventArgs<T> : EventArgs
{
    public static readonly CollectionEventArgs<T> Empty = new CollectionEventArgs<T>();

    public CollectionEventArgs(params T[] items)
    {
        this.Items = new ReadOnlyCollection<T>(items);
    }

    public CollectionEventArgs(IEnumerable<T> items)
    {
        this.Items = new ReadOnlyCollection<T>(items.ToArray());
    }

    public ReadOnlyCollection<T> Items
    {
        get;
        private set;
    }
}
+3

, . , , , - . .NET, , .

, . , , , . , , .

+1

-

public class EventArgs<TValue> : EventArgs
{
    public TValue Value { get; }

    /* ... */
}

?

Value ?

, ​​. , .

+1
source

All Articles