Override property setter and getter

笑着哭i 提交于 2019-11-30 09:08:38

问题


Is there a way to override the setter and getter of an auto property with an attribute?

like this:

[CustomAttribute]
public int Value { get; set; }

...

public class CustomAttibute : Attribute
{
    public override NotExistingPropertySetter(object value)
    {    
        if (((int)value) < 10 )
        {
            value = 10;
        }
    }
}

回答1:


No. An attribute is never "executed". You would need to build a construct looking for the attribute and doing something if it's found.

AoP (aspect oriented programming) in .NET is only possible by using third party software like PostSharp.




回答2:


I believe it will achieve this with AOP



来源:https://stackoverflow.com/questions/21733428/override-property-setter-and-getter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!