问题
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