简体   繁体   English

如何查看 c# 中 ref 属性的属性更改?

[英]How to watch for a property changed on a ref property in c#?

If you have a reference property like this:如果您有这样的参考属性:

public class Foo {
    public ref Vector2 Bar {
        get => ref bar;
    }
    private Vector2 bar;
}

Is there a way to listen for changes when Bar is modified like this? Bar这样修改的时候有没有办法监听变化呢?

    set {
        position = value;
        NotifyPositionChanged(value);
    }

Ideally all of the notify functionality would be encapsulated within the Foo class but none of the solutions I can think of maintain that encapsulation.理想情况下,所有通知功能都将封装在 Foo class 中,但我能想到的解决方案都没有维护该封装。

Is this impossible or am I missing something?这是不可能的还是我错过了什么?

You cannot define setter for a reference type property in C#.您不能为 C# 中的引用类型属性定义 setter。

Though you can create a method which will do the job like.虽然您可以创建一个方法来完成这项工作。

public void UpdateBar(Vector2 value)
{
    bar = value;
    NotifyPositionChanged(value);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM