2009-05-11 13 views
3

注:これはなぜではなく、外にそれのイベントを宣言するタイプ内のイベントのMulticastDelegateメンバーにアクセスすることができる午前WebBrowser Event Properties?異なるタイプのメンバーであるイベントのメンバーにアクセスできないのはなぜですか?

に触発されたのですか?例えば

using System; 

class Bar 
{ 
    public static event Action evt; 
} 

class Program 
{ 
    static event Action foo; 
    static Bar bar; 

    static void Main() 
    { 
     // this works 
     Delegate[] first = foo.GetInvocationList(); 

     // This does not compile and generates the following 
     // error: 
     // 
     // The event 'Bar.evt' can only appear on the 
     // left hand side of += or -= (except when used 
     // from within the type 'Bar') 
     Delegate[] second = bar.evt.GetInvocationList(); 
    } 
} 

私はこれは私が見ていないですという非常に単純なものである感覚を得ます。

答えて

5

イベントはフィールドとその値を公開しません。プロパティがゲッターとセッターだけを公開する方法と同様に、サブスクリプションメソッドとサブスクライブ解除メソッドを公開します。

クラス外にあるすべてイベントは、イベントを購読または購読中止することができます。それがカプセル化の目的です。

詳細については、event articleを参照してください。

+0

+1意味があります - ありがとう! –

関連する問題