1

我正在编写一个库,我的客户正在使用我的库。

我的客户创建了从我的基类派生的自己的类。

我可以检测到我的客户的类属性已更改吗?

我不希望我的客户实施 INotifyPropertyChanged。

我还将反射用于其他目的。为什么反射无法检测到属性更改状态?

我的图书馆代码:

public class BaseClass
{
    public void ChildPropertyChanged(propinfo...)
    {
        ...
    }
}

客户代码:

public class MyClass : BaseClass
{
    public string Name {get;set;}
}
4

2 回答 2

0

Your clients must collaborate, meaning they will have to write code to help you catch the change of properties, as in INotifyPropertyChanged clients (implementers) must raise an event (RaisePropertyChanged);

If you think about it, if what you are asking was possible, than INotifyPropertyChanged was not necessary..

One possible solution for you is to define an event in the base class, and guide your clients to raise it in their properties getters, or even better - calling a base class handler method.

于 2014-01-05T13:50:26.850 回答
0

您可以实现模板模式。

基类。

public class BaseClass
{
    protected string name;

    private void NameChanged()
    {
         ...
    }

    public void SetName(string value)
    {
        this.name = value;
        this.NameChanged();
    }
}

派生类:

public class MyClass : BaseClass
{
    public string Name 
    {
        get 
        {
            return this.name; 
        }

        set 
        {
            this.SetName(value);
        }
    }
}

虽然这样做消除了派生类中对属性的需求——它可以使用基类方法进行更改SetName

于 2014-01-05T14:06:24.567 回答