如何捕获成员变量的变化? (C#)

发布于 2024-11-29 15:27:39 字数 275 浏览 0 评论 0原文

这似乎是该语言的基础知识,但我不明白这是如何在 .Net 中实现的。我在类中有一个成员变量,例如 bool _isCommited。我希望每当 _isCommissed 为 true 时就会发生一些事情。像这样的事情:

     //Whenever _isCommitted == true()
     {
         Foo()
     }

基本上就像一个事件,但这里它是我的变量。怎样做?非常感谢..

This seems to be basics of the language, but I do not understand how is this accomplished in .Net. I have a member variable in a class, say a bool _isCommitted. I want something to happen whenever _isCommitted is true. Something like this:

     //Whenever _isCommitted == true()
     {
         Foo()
     }

Basically like an event, but here it is my variable. How to? Many thanks..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2024-12-06 15:27:39

这通常是通过属性和支持私有字段来完成的。您需要确保您只能通过该物业进入。

private bool _isCommitted;

public bool IsCommitted
{
  get { return _isCommitted; }
  set
  {
     if(value)
     {
        //do something
     }

     _isCommitted = value;
  }
}

This is normally done through properties and a backing private field. You need to ensure you only ever access through the property.

private bool _isCommitted;

public bool IsCommitted
{
  get { return _isCommitted; }
  set
  {
     if(value)
     {
        //do something
     }

     _isCommitted = value;
  }
}
浅忆 2024-12-06 15:27:39

在最基本的层面上,您可以在您的类中创建一个事件:

public delegate void MyHandler(bool b);
public event MyHandler CommittedChanged;

现在人们可以像这样订阅您的事件:

public void SomeHandlerMethod(bool b) { ... }

...

someInstance.CommittedChanged += SomeHandlerMethod;
someInstance.CommittedChanged += ASecondHandlerMethod;
someInstance.CommittedChanged += x => { /* inline handler using lambda */ };

用户可以通过这种方式取消注册他的事件处理程序:

someInstance.CommittedChanged -= SomeHandlerMethod;

无论您决定更改变量,您都会跟进它:

if (CommittedChanged != null) CommittedChanged(_isCommitted);

这将呼叫所有在您的活动中注册了功能的人。

话虽如此,您可以做很多改进。首先,将 _isCommited 放入属性中,并在其 setter 中执行事件回调。这样,您就不会忘记调用处理程序。

public IsCommitted { 
   get { return _isCommitted; } 
   set {  
     _isCommitted = value; 
     if (CommittedChanged != null) CommittedChanged(_isCommitted);
   }
}

此处了解有关事件的更多信息。

这足以让你继续前进。但是,如果您进一步深入研究 C# 框架,您将发现在 System.ComponentModel 命名空间内使用此事件框架的标准化方法。具体来说,接口INotifyPropertyChanged,它巧妙地与一个更通用的事件系统联系在一起,该系统也可以很好地与微软自己的一些技术(例如WPF)配合使用,允许GUI元素自动获取类的更改。 此处INotifyPropertyChanged 的更多信息>。

At the most basic level, you can create an event in your class:

public delegate void MyHandler(bool b);
public event MyHandler CommittedChanged;

Now people can subscribe to your event like so:

public void SomeHandlerMethod(bool b) { ... }

...

someInstance.CommittedChanged += SomeHandlerMethod;
someInstance.CommittedChanged += ASecondHandlerMethod;
someInstance.CommittedChanged += x => { /* inline handler using lambda */ };

A user can unregister his event handler this way:

someInstance.CommittedChanged -= SomeHandlerMethod;

And wherever you decide to change your variable, you will follow it up with:

if (CommittedChanged != null) CommittedChanged(_isCommitted);

This will call everyone who has registered a function with your event.

Having said this, there are plenty of improvements that you can do. First, make _isCommitted into a property, and do the event callback in its setter. This way, you won't forget to call the handlers.

public IsCommitted { 
   get { return _isCommitted; } 
   set {  
     _isCommitted = value; 
     if (CommittedChanged != null) CommittedChanged(_isCommitted);
   }
}

Read more about events here.

This is enough to get you going. However, if you delve further into the C# framework, you will find a standardized way of using this event framework inside of the System.ComponentModel namespace. Sepcifically, the interface INotifyPropertyChanged, which ties neatly into a more generic event system that also plays well with some of Microsoft's own technologies, such as WPF, allowing GUI elements to pick up on changes to your class automatically. Read more about INotifyPropertyChanged here.

開玄 2024-12-06 15:27:39

您基本上需要 PropertyChangedEvent PropertyChangedEventHandler Delegate

You basically need PropertyChangedEvent PropertyChangedEventHandler Delegate

纵性 2024-12-06 15:27:39

我认为 C# 属性 就是你的需要。

private bool _isCommitted;

    public bool IsCommitted
    {
        get { return _isCommitted; }
        set { if(value){/*DO SOMETHING HERE*/}
            _isCommitted = value; }
    }

I think C# properties is what you need.

private bool _isCommitted;

    public bool IsCommitted
    {
        get { return _isCommitted; }
        set { if(value){/*DO SOMETHING HERE*/}
            _isCommitted = value; }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文