0

リフレクションを使用してオブジェクトの属性が変更されたときに通知を受け取りたい。

これは、mjpeg.dll のクラスの 1 つです。

public class MJPEGConfiguration : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string psw;
        public string   password
        {
            get
            {
                return psw;
            }
            set
            {
                psw = value;
                OnPropertyChanged("PSW");
            }
        }

        public virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

camera.cs で、MJPEGConfiguration オブジェクトを「オブジェクト構成」に設定し、このオブジェクトに PropertyChanged イベントを追加します。

    public object Configuration
    {
        get 
        {
            return configuration; 
        }
        set
        {
            configuration = value;

            Type t = configuration.GetType(); //t is the type of "MJPEGConfiguration"
            EventInfo ei = t.GetEvent("PropertyChanged");
            MethodInfo mi = this.GetType().GetMethod("My_PropertyChanged");
            Delegate dg = Delegate.CreateDelegate(ei.EventHandlerType, mi);
            ei.AddEventHandler(configuration, dg);
        }
    }

    public void My_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {

    }

しかし、「Delegate dg = ....」の行で ArgumentException(Error binding to Target Method) が発生しています。この問題を解決するにはどうすればよいですか? または、これを行う正しい方法はありますか?

4

1 に答える 1

1

呼び出している .CreateDelegate のオーバーロードは、静的メソッドにバインドしようとしています。インスタンス メソッドの場合は、次のようにします。

Delegate dg = Delegate.CreateDelegate(et, value, mi);
于 2011-03-06T05:16:50.123 に答える