0

我想在使用反射更改对象属性时收到通知。

这是 mjpeg.dll 中的一类:

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 回答