夏普后合约范围

本文关键字:范围 夏普 | 更新日期: 2023-09-27 18:18:11

是否有可能实现这样的东西与PostSharp和合同?

public class Something
{
    int number = 10;
    public void Remove([Range(1,this.number)] int remove)
    {
        number -= remove;
    }
    public void Add(int add)
    {
        number += add;
    }
}

夏普后合约范围

c#编译器将不允许您以这种方式应用[Range]属性-您将收到一个构建错误,指出"属性参数必须是常量表达式,typeof表达式或数组创建表达式"。

解决方法是创建一个接受字段名作为参数的方面。然后将该字段导入到aspect中,这样您就可以读取当前的最大值。

[Serializable]
public class MyRangeAttribute : LocationContractAttribute,
                                ILocationValidationAspect<int>,
                                IInstanceScopedAspect,
                                IAdviceProvider
{
    [NonSerialized]
    private object instance;
    [NonSerialized]
    private string maxValueFieldName;
    private int minValue;
    public ILocationBinding maxValueFieldBinding;
    public MyRangeAttribute(int minValue, string maxValueFieldName)
    {
        this.minValue = minValue;
        this.maxValueFieldName = maxValueFieldName;
    }
    public Exception ValidateValue(int value, string locationName, LocationKind locationKind)
    {
        int maxValue = (int) this.maxValueFieldBinding.GetValue(ref this.instance, Arguments.Empty);
        if (value < minValue || value > maxValue)
            return new ArgumentOutOfRangeException(locationName);
        return null;
    }
    public IEnumerable<AdviceInstance> ProvideAdvices(object targetElement)
    {
        FieldInfo maxValueField = ((LocationInfo)targetElement).DeclaringType
            .GetField( this.maxValueFieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );
        yield return new ImportLocationAdviceInstance(
            typeof (MyRangeAttribute).GetField("maxValueFieldBinding"),
            new LocationInfo(maxValueField));
    }
    public object CreateInstance( AdviceArgs adviceArgs )
    {
        MyRangeAttribute clone = (MyRangeAttribute) this.MemberwiseClone();
        clone.instance = adviceArgs.Instance;
        return clone;
    }
    public void RuntimeInitializeInstance()
    {
    }
}

你可以这样应用这个方面:

public class Something
{
    private int number = 10;
    public void Remove([MyRange(1, "number")] int remove)
    {
        number -= remove;
    }
    public void Add(int add)
    {
        number += add;
    }
}