修改时间戳会更新类属性的任何更改

发布于 2024-10-14 11:56:35 字数 576 浏览 2 评论 0原文

我有点不确定如何表达这个问题,所以如果这是重复的,请原谅我。

基本上我想在每次更改属性时调用 UpdateModifiedTimestamp 。这只是我很快写的一个示例类,但应该解释我想要实现的目标。

每次更改名字、姓氏或电话号码时,都应更新 ModifiedOn 属性。

public class Student {
   public DateTime ModifiedOn { get; private set; }
   public readonly DateTime CreatedOn;
   public string Firstname { set; get; }
   public string Lastname { set; get; }
   public string Phone { set; get; }

   public Student() {
      this.CreatedOn = DateTime.Now();
   }

   private void UpdateModifiedTimestamp() {
      this.ModifiedOn = DateTime.Now();
   }
}

I'm a bit unsure on how I'd word this question, so pardon me if this is duplicate.

Basically I want to call UpdateModifiedTimestamp everytime a property is changed. This is just a sample class I wrote up pretty quickly, but should explain what I'm trying to achieve.

Everytime Firstname, Lastname, or Phone is changed it should update the ModifiedOn property.

public class Student {
   public DateTime ModifiedOn { get; private set; }
   public readonly DateTime CreatedOn;
   public string Firstname { set; get; }
   public string Lastname { set; get; }
   public string Phone { set; get; }

   public Student() {
      this.CreatedOn = DateTime.Now();
   }

   private void UpdateModifiedTimestamp() {
      this.ModifiedOn = DateTime.Now();
   }
}

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

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

发布评论

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

评论(2

陌生 2024-10-21 11:56:35

您所描述的内容听起来非常接近通常通过 INotifyPropertyChanged 接口。实现这个接口将为您的问题提供更通用的解决方案:

public class Student :INotifyPropertyChanged
{
   public string Firstname { set; get; }
   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged(String info)
   {
      UpdateModifiedTimestamp(); // update the timestamp
      if (PropertyChanged != null)
      {
          PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
   }

   string _firstname;
   public string Firstname  //same for other properties
   {
     get
     {
         return _firstname;
     }

     set
     {
        if (value != _firstname)
        {
            _firstname = value;
            NotifyPropertyChanged("Firstname");
        }
     }
   }
 }

这种方法也将使更改通知可供您的类的使用者使用,如果这就是您的目标,那么不同的解决方案可能会更好。

What you are describing sounds pretty close to the property change notification usually done via the INotifyPropertyChanged interface. Implementing this interface would give you a little more generic solution to your problem:

public class Student :INotifyPropertyChanged
{
   public string Firstname { set; get; }
   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged(String info)
   {
      UpdateModifiedTimestamp(); // update the timestamp
      if (PropertyChanged != null)
      {
          PropertyChanged(this, new PropertyChangedEventArgs(info));
      }
   }

   string _firstname;
   public string Firstname  //same for other properties
   {
     get
     {
         return _firstname;
     }

     set
     {
        if (value != _firstname)
        {
            _firstname = value;
            NotifyPropertyChanged("Firstname");
        }
     }
   }
 }

This approach would make the change notification available to consumers of your class as well, if that's what you are shooting for, a different solution probably would be preferable.

ゞ记忆︶ㄣ 2024-10-21 11:56:35

不确定这是否是最好的方法,但可以执行此操作的一种方法是,在属性的三个设置器中调用 UpdateModifiedTimeStamp() 方法。

例如:

public string _firstName;
public string Firstname 
{ 
  get { return this._firstName; }
  set
  {
    this._firstName = value;
    this.UpdateModifiedTimestamp();
  }
}

同样,对 LastnamePhone 属性也执行相同的操作。

Not sure if this is the best way, but one way you could do this is, call the UpdateModifiedTimeStamp() method in the three setters of your properties.

eg:

public string _firstName;
public string Firstname 
{ 
  get { return this._firstName; }
  set
  {
    this._firstName = value;
    this.UpdateModifiedTimestamp();
  }
}

Similarly, do the same for Lastname, and Phone properties as well.

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