1

リフレクションを使用してプライベートメソッドを取得するのに問題があります。BindingFlags.NonPublicおよびBindingFlags.Instanceを使用しても、機能しません。HandleClientDrivenStatePropertyChangedは、CreateRadioPropertyInstancesメソッドと同じクラスで定義されます。

 class Program
 {
      static void Main(string[] args)
      {
         RadioPropertiesState state = new RadioPropertiesState();
      }
 }

 internal class RadioPropertiesState : BaseRadioPropertiesState
 {
 }

 internal class BaseRadioPropertiesState
 {
     public BaseRadioPropertiesState()
     {
          CreateRadioPropertyInstances();
     }

     private void CreateRadioPropertyInstances()
     {
          // get the method that is subscribed to the changed event
          MethodInfo changedEventHandlerInfo = GetType().GetMethod(
               "HandleClientDrivenStatePropertyChanged",
               BindingFlags.NonPublic | BindingFlags.Instance | 
               BindingFlags.IgnoreCase);
     }

     private void HandleClientDrivenStatePropertyChanged
         (object sender, EventArgs e)
     {
     }
}

GetMethodはnullを返します。何が問題になる可能性がありますか?

[編集されたコード]

4

2 に答える 2

4

問題は、私のコメントで提案したとおりです。オブジェクトの実行時間タイプRadioPropertiesStateに基づいてメソッドを見つけようとしています。これは...ですが、そのタイプで宣言されていないか、表示されていません。

GetMethod通話を次のように変更します。

MethodInfo changedEventHandlerInfo = typeof(BaseRadioPropertiesState)
                                         .GetMethod(...)

そしてそれはうまくいきます。

于 2012-03-17T11:58:04.137 に答える
0

GetMethodプライベートメンバーを取得するには、派生型ではなく、宣言された正確な型を呼び出す必要があります。

BindingFlags.FlattenHierarchyメソッドはプライベートであるため、ここでは機能しません。

于 2012-03-17T11:57:43.600 に答える