简体   繁体   English

如何覆盖C#中设置的自动属性

[英]how to override set in C# of automatic properties

I want to use auto-implemented properties, but at the same time I want to call a method every time the property is changed.我想使用自动实现的属性,但同时我想在每次更改属性时调用一个方法。

public float inverseMass
{
    get;
    set
    {
        onMassChanged();
        inverseMass = value;
    }
}

Does the previous code make a recursive call to the property?前面的代码是否对属性进行了递归调用? If it does, how to implement this?如果是这样,如何实现?

If you provide your own get/set then you need to provide your own storage for the variable.如果您提供自己的 get/set,那么您需要为变量提供自己的存储空间。

private float _inverseMass;

public float inverseMass
{
    get { return _inverseMass; }
    set
    {
        _inverseMass = value;
        onMassChanged();
    }
}

Use a backing field instead:改用支持字段:

public float inverseMass
{
    get
    {
        return _inverseMass;
    }
    set
    {
        _inverseMass = value;
        onMassChanged();
    }
}

private float _inverseMass;

I once read somewhere that the C# guys decided that they wanted to cover the broad of the usage scenarios with those automatic properties, but for special cases you have to go back to good old backing fields.我曾经在某处读到 C# 人员决定他们想用这些自动属性覆盖广泛的使用场景,但对于特殊情况,您必须回到良好的旧支持字段。

(To be honest, I believe that Jon Skeet told them how to implement them) (老实说,我相信 Jon Skeet 告诉他们如何实施它们)

You would need to break the above out in to private/public members so you don't get recursive problems:您需要将上述内容分解为私有/公共成员,以免出现递归问题:

private float _inverseMass;
public float inverseMass
{
  get { return this._inverseMass; }
  set { this._inverseMass = value; onMassChanged(); }
}

However, have you looked in to the INotifyPropertyChanged interface?但是,您是否查看过INotifyPropertyChanged接口? It does pretty much what you're looking for, and depending on what you're writing it could be supported natively.它几乎可以满足您的需求,并且根据您正在编写的内容,它可能会得到本机支持。

public class MyClass : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;
  private void NotifyPropertyChanged(String property)
  {
    var event = this.PropertyChanged;
    if (event != null)
    {
      event(this, new PropertyChangedEventArgs(property));
    }
  }

  private float _inverseMass;
  public float inverseMass
  {
    get { return this._inverseMass; }
    set { this._inverseMass = value; NotifyPropertyChanged("inverseMass"); }
  }
}

You can't do that in C#.你不能在 C# 中做到这一点。

Its either automatic property or property with backing field.它要么是自动属性,要么是带有支持字段的属性。

That doesn't compile, so it's hard to say if it's recursive or not.那不能编译,所以很难说它是否是递归的。

You cannot specify only one part of an automatic property.您不能只指定自动属性的一部分。 You will need to use a backing-field in this case:在这种情况下,您将需要使用支持字段:

private float _inverseMass;
public float inverseMass 
{ 
    get { return _inverseMass; }
    set 
    { 
        onMassChanged(); 
        _inverseMass = value; 
    } 
}

Though, an aspected-oriented programming framework like PostSharp may be able to help you with this.不过,像PostSharp这样的面向方面的编程框架可能能够帮助你解决这个问题。

The correct answers are given above, but in case you wanted to do this quickly in Visual Studio, you can use Code Snippets instead to generate the property for you.上面给出了正确答案,但如果您想在 Visual Studio 中快速执行此操作,则可以改用代码片段为您生成属性。

Have a read of this article which should explain it a bit better: http://www.roelvanlisdonk.nl/?p=1007阅读这篇文章应该会更好地解释它: http : //www.roelvanlisdonk.nl/?p=1007

If you want, you can even create your own code snippet to add the repetitive code.如果需要,您甚至可以创建自己的代码片段来添加重复代码。 You could even use it to quickly generate the INotifyPropertyChanged implementation as mentioned above.您甚至可以使用它来快速生成上面提到的 INotifyPropertyChanged 实现。

Use a private variable as your back up field for the public property.使用私有变量作为公共属性的备份字段。

private float inverseMass;

public float InverseMass{

   set
   {
        onMassChanged();
        inverseMass=value;
   }
   get
   {
      return inverseMass;
   }

}
public float inverseMass
{
    get;
    set
    {
        if (inverseMass != value)
        {
            inverseMass = value;
            onMassChanged();
        }
    }
}  

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

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