Do something when a property is set
imagine this:
public class Foo
{
private IList<string> WasSet;
public 开发者_如何转开发string Prop1 {get;set;}
public bool Prop2 {get;set;}
...
public int Prop900 {get;set;}
}
I would like to do:
WasSet.Add(PropertyName);
when a property is set.
is this possible without getting rid of auto-properties ?
You can't do this with auto properties, as the compiler is generating the appropriate backing field and accessors for you. You'd need to write the property yourself and handle the set
functionality to update your list.
No - automatic properties are always just trivial implementations backed by a simple field, with no "triggers" etc. You'll have to write a full property. You may want to implement INotifyPropertyChanged
to make it more general-purpose.
精彩评论