简体   繁体   English

是否可以评估调用set属性的事件?

[英]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.

相关问题 是否可以使用类似于自动属性 ({ get; set; }) 的语法来声明引发 PropertyChanged 事件的属性? - Is it possible to declare a property raising PropertyChanged event using a syntax similar to an auto-property ({ get; set; })? 我能否以某种方式评估表达式以确定并可能设置null属性? - Can I evaluate an expression in a way to determine and possibly set a property that is null? 是否可以动态设置%property {PropertyName}? - It is possible to set %property{PropertyName} dynamically? 是否可以动态设置条件的属性? - Is it possible to set the property of the condition dynamically? 是否可以通过CollectionChanged事件在ObservableCollection属性上进行NotifyChanged? - Is it possible to NotifyChanged on an ObservableCollection property via CollectionChanged event? 在.NET中,是否可以根据函数调用来设置事件 - In .NET, is it possible to set Events based on calling of a function 无法评估属性表达式 - Can not Evaluate property expression 更改属性中的元素时调用属性的set方法 - Calling the set method of a property when changing elements within that property 如何评估添加到jQuery Full Calendar事件中的属性? - How do I evaluate a property that I added to a jQuery Full Calendar event? 从日历事件中设置usercontrol属性 - set usercontrol property from event from calendar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM