[英]Is it possible, to evaluate the event of calling a set property?
Let's say, we have an ordinary C# class with one auto get/set property. 假设我们有一个带有一个自动获取/设置属性的普通C#类。
public class Entity
{
public String SomeProperty {get;set;}
}
Is there any event, that is raised and that I can evaluate, when the set method of the SomeProperty is called? 调用SomeProperty的set方法时,是否有任何引发并且可以评估的事件?
Is something like this possible in any way, maybe reflection?: 这样的事情是否有可能发生,也许是反思?:
Pseudo Code, NO REAL CODE: 伪代码,无真实代码:
Entity e = new Entity();
e.SomeProperty.SetterCalled += OnSetterCalled;
private void OnSetterCalled(Sender propertyinfo)
{
propertyinfo pi = propertyinfo;
Console.Write (pi.Name);
}
I know, I could use CallerMember, but then I had to change the auto property. 我知道,我可以使用CallerMember,但是后来我不得不更改auto属性。
No, there is no way to do this. 不,没有办法做到这一点。
The setter is just this: 设置者就是这样:
_backingVariable = value;
Assignment does not inherently invoke any methods. 分配不会固有地调用任何方法。 As it stands, there is no way to raise an event during a property set utilizing an auto-property.
就目前而言,在利用自动属性进行属性设置期间无法引发事件。
You can change the code at compile time using something like the technique described in: https://stackoverflow.com/a/18002490/1783619 您可以使用类似https://stackoverflow.com/a/18002490/1783619中所述的技术在编译时更改代码。
But otherwise, there is no way to do this. 但是否则,将无法执行此操作。
I would recommend looking into: 我建议您调查:
INotifyPropertyChanged
Here is a great walkthrough: 这是一个很棒的演练:
Implementing INotifyPropertyChanged - does a better way exist? 实现INotifyPropertyChanged-是否存在更好的方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.