覆盖属性setter和getter属性、setter、getter

2023-09-04 03:41:44 作者:薄凉旧情人

有没有办法覆盖的汽车属性的setter和getter和一个属性?

是这样的:

  [CustomAttribute]
公共int值{获得;组; }
 

...

 公共类CustomAttibute:属性
{
    公众覆盖NotExistingPropertySetter(对象的值)
    {
        如果(((int)的值)小于10)
        {
            值= 10;
        }
    }
}
 

解决方案 访问器属性之getter setter函数

没有。从不执行的属性。您将需要建立一个结构寻找属性,做的东西,如果它的发现。

AOP(面向方面​​编程)在.NET中只能使用第三方软件,如PostSharp。

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;
        }
    }
}

解决方案

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.

 
精彩推荐
图片推荐