C# 物业更改时取消订阅/重新订阅事件

C# 物业更改时取消订阅/重新订阅事件,c#,events,C#,Events,我经常写这样的代码: MyObject property; MyObject Property { get { return property; } set { if (property != null) property.Changed -= property_Changed; // unsubscribe from the old value property = value;

我经常写这样的代码:

MyObject property;
MyObject Property
{
    get { return property; }
    set {
            if (property != null)
                property.Changed -= property_Changed; // unsubscribe from the old value
            property = value;
            property.Changed += property_Changed; // subscribe to the new value
        }
}
我正在寻找一种优雅的方式来自动取消订阅。
有什么想法吗?

这是你试图实现的想法中最优雅的。当然,您有一个逻辑错误,当传入时,该值可能为null,因此property.Changed+=property\u Changed将爆炸


基本上,如果您分配了一个新对象,您希望取消订阅旧元素的事件并附加到新元素事件

这是你试图实现的想法所能达到的最优雅的境界。当然,您有一个逻辑错误,当传入时,该值可能为null,因此property.Changed+=property\u Changed将爆炸


基本上,如果您分配了一个新对象,您希望取消订阅旧元素的事件并附加到新元素事件

可能是这样的,如果您确实需要取消订阅/订阅每次属性更改:

MyObject Property
{
    get { return property; }
    set {
            //unsubscribe always, if it's not null 
            if(property !=null)
               property.Changed -= property_Changed;

            //assign value 
            property = value;

            //subscribe again, if it's not null
            if (property != null)                            
                property.Changed += property_Changed; 

        }
}

可能是这样的,如果您确实需要取消订阅/订阅每次属性更改:

MyObject Property
{
    get { return property; }
    set {
            //unsubscribe always, if it's not null 
            if(property !=null)
               property.Changed -= property_Changed;

            //assign value 
            property = value;

            //subscribe again, if it's not null
            if (property != null)                            
                property.Changed += property_Changed; 

        }
}

也许使用扩展方法可能是您想要的

你可以试试这样的

private MyProperty property;
public MyProperty Property
{
    get { return property; }
    set { property.SubPubValue(value, property_Changed); }
}

private static void property_Changed(object sender, PropertyChangedEventArgs e)
{
    throw new NotImplementedException();
}

public static class Extensions
{
    public static void SubPubValue<T>(this T value, T setValue, PropertyChangedEventHandler property_Changed) where T : MyProperty
    {
        if (setValue != null)
            value.PropertyChanged -= property_Changed;
        value = setValue;
        if (setValue != null)
            value.PropertyChanged += property_Changed;
    }
}

也许使用扩展方法可能是您想要的

你可以试试这样的

private MyProperty property;
public MyProperty Property
{
    get { return property; }
    set { property.SubPubValue(value, property_Changed); }
}

private static void property_Changed(object sender, PropertyChangedEventArgs e)
{
    throw new NotImplementedException();
}

public static class Extensions
{
    public static void SubPubValue<T>(this T value, T setValue, PropertyChangedEventHandler property_Changed) where T : MyProperty
    {
        if (setValue != null)
            value.PropertyChanged -= property_Changed;
        value = setValue;
        if (setValue != null)
            value.PropertyChanged += property_Changed;
    }
}

您的意思是检查整个setter是否为null?您的意思是检查整个setter是否为null?