イベント Action<> とイベント EventHandler<> の違い 質問する

イベント Action<> とイベント EventHandler<> の違い 質問する

event Action<>宣言することと することの間には何か違いがありますかevent EventHandler<>

実際にイベントを発生させたオブジェクトが何であるかは問題ではないと仮定します。

例えば:

public event Action<bool, int, Blah> DiagnosticsEvent;

public event EventHandler<DiagnosticsArgs> DiagnosticsEvent;

class DiagnosticsArgs : EventArgs
{
    public DiagnosticsArgs(bool b, int i, Blah bl)
    {...}
    ...
}

どちらの場合も使い方はほぼ同じです。

obj.DiagnosticsEvent += HandleDiagnosticsEvent;

event EventHandler<>パターンに関して気に入らない点がいくつかあります。

  • EventArgs から派生した追加の型宣言
  • オブジェクトソースの強制的な受け渡し – 多くの場合、誰も気にしない

コードが増えるということは、明確な利点がないまま、保守するコードが増えることを意味します。

その結果、私はevent Action<>

ただし、Action<> に型引数が多すぎる場合にのみ、追加のクラスが必要になります。

ベストアンサー1

これまでの回答のいくつかに基づいて、私の回答を 3 つの領域に分類します。

First, physical limitations of using Action<T1, T2, T2... > vs using a derived class of EventArgs. There are three: First, if you change the number or types of parameters, every method that subscribes to will have to be changed to conform to the new pattern. If this is a public facing event that 3rd party assemblies will be using, and there is any possiblity that the event args would change, this would be a reason to use a custom class derived from event args for consistencies sake (remember, you COULD still use an Action<MyCustomClass>) Second, using Action<T1, T2, T2... > will prevent you from passing feedback BACK to the calling method unless you have a some kind of object (with a Handled property for instance) that is passed along with the Action. Third, you don't get named parameters, so if you're passing 3 bool's an int, two string's, and a DateTime, you have no idea what the meaning of those values are. As a side note, you can still have a "Fire this event safely method while still using Action<T1, T2, T2... >".

Secondly, consistency implications. If you have a large system you're already working with, it's nearly always better to follow the way the rest of the system is designed unless you have an very good reason not too. If you have publicly facing events that need to be maintained, the ability to substitute derived classes can be important. Keep that in mind.

Thirdly, real life practice, I personally find that I tend to create a lot of one off events for things like property changes that I need to interact with (Particularly when doing MVVM with view models that interact with each other) or where the event has a single parameter. Most of the time these events take on the form of public event Action<[classtype], bool> [PropertyName]Changed; or public event Action SomethingHappened;. In these cases, there are two benefits. First, I get a type for the issuing class. If MyClass declares and is the only class firing the event, I get an explicit instance of MyClass to work with in the event handler. Secondly, for simple events such as property change events, the meaning of the parameters is obvious and stated in the name of the event handler and I don't have to create a myriad of classes for these kinds of events.

おすすめ記事