您可以触发具有自动属性的事件吗?

发布于 2024-09-30 11:43:41 字数 682 浏览 2 评论 0原文

我想知道是否可以使用自动属性并且仍然能够在属性更改时触发事件。这是我目前的课程。 (当然,实际的 User 类有更多的属性/字段)。

public delegate void UserEventHandler(object sender, EventArgs e);

public class User
{
    public event UserEventHandler Changed;

    private string _UserName;
    public string UserName
    {
        get
        {
            return _UserName;
        }
        private set
        {
            _UserName = value;
            this.OnChanged(EventArgs.Empty);
        }
    }

    protected void OnChanged(EventArgs e)
    {
        if (Changed != null)
        {
            Changed(this, e);
        }
    }
}

所以我想知道是否有一种方法可以利用自动属性,并且仍然能够触发 OnChanged 事件。 换句话说:半自动属性可能吗?

I was wondering if I can use automatic properties and still be able to fire events on property changed. Here are my current classes. (The actual User class got way more properties/fields of course).

public delegate void UserEventHandler(object sender, EventArgs e);

public class User
{
    public event UserEventHandler Changed;

    private string _UserName;
    public string UserName
    {
        get
        {
            return _UserName;
        }
        private set
        {
            _UserName = value;
            this.OnChanged(EventArgs.Empty);
        }
    }

    protected void OnChanged(EventArgs e)
    {
        if (Changed != null)
        {
            Changed(this, e);
        }
    }
}

So I was wondering if there is a way I could take advantage of the automatic properties and still be able to fire the OnChanged events.
In other words : Are semi-automatic properties possible?

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

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

发布评论

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

评论(3

千里故人稀 2024-10-07 11:43:41

您可以使用PostSharp
示例

You can use PostSharp.
Example

感情废物 2024-10-07 11:43:41

聚会已经很晚了,但这个问题仍然出现在谷歌上。

有一个包的工作方式与 PostSharp 示例大致相同,但免费:Fody.Propertychanged

该项目的自述文件及其链接到的 wiki 页面对其进行了很好的解释。

Very late to the party, but this question still appears on google.

There's a package which works in much the same way as the PostSharp example, but is free: Fody.Propertychanged.

The project's README, and the wiki pages it links to, do a very good job of explaining it.

小鸟爱天空丶 2024-10-07 11:43:41

我对您的代码进行了一些修改,以便可以访问事件并使用现成的 EventHandler。

    public class User
    {
        public event EventHandler AgeChanged;     
        private string _UserName;
        public string UserName
        {
            get
            {
                return _UserName;
            }
             set  
            {
                _UserName = value;
                this.OnAgeChanged(this,EventArgs.Empty);
            }
        }

        protected virtual void OnAgeChanged(object sender, EventArgs e)
        {
            if (AgeChanged != null)
            {
                AgeChanged(sender, e);
            }
        }
   }

如何设置事件:

      var user = new User();
     //subscribe to events
     user.AgeChanged+= (s,e) => Console.WriteLine("UserNamed changed to {0}",user.UserName);

    //modify UserName and now event is fired
     user.UserName="Jack";

查看工作演示

I modified your code a little for the event can be accessed and using the ready made EventHandler.

    public class User
    {
        public event EventHandler AgeChanged;     
        private string _UserName;
        public string UserName
        {
            get
            {
                return _UserName;
            }
             set  
            {
                _UserName = value;
                this.OnAgeChanged(this,EventArgs.Empty);
            }
        }

        protected virtual void OnAgeChanged(object sender, EventArgs e)
        {
            if (AgeChanged != null)
            {
                AgeChanged(sender, e);
            }
        }
   }

How to Set Events:

      var user = new User();
     //subscribe to events
     user.AgeChanged+= (s,e) => Console.WriteLine("UserNamed changed to {0}",user.UserName);

    //modify UserName and now event is fired
     user.UserName="Jack";

see a working demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文