C# 如何设置纯度的异常属性?

C# 如何设置纯度的异常属性?,c#,.net,code-contracts,C#,.net,Code Contracts,我有像bellow这样的类,我只想让x在Foo方法中更改,而不想更改其他属性。我不能像下面的示例那样使用[Pure],因为它会锁定所有属性: public class Test { private int x,y,z; //number of these properties is large [Pure] public void Foo() { //only x must be allowed to change } } 除了x,我不

我有像bellow这样的类,我只想让
x
Foo
方法中更改,而不想更改其他属性。我不能像下面的示例那样使用
[Pure]
,因为它会锁定所有属性:

public class Test
{
    private int x,y,z; //number of these properties is large

    [Pure]
    public void Foo()
    {
        //only x must be allowed to change
    }
}
除了
x
,我不想对所有其他属性使用这样的东西:

Contract.Ensures(Contract.OldValue<int>(y) == y);
Contract.Ensures(Contract.OldValue<int>(z) == z);
...//and for other large number of properties
Contract.provides(Contract.OldValue(y)=y);
合同价值(合同价值(z)=z);
…//对于其他大量属性

有什么方法可以做到这一点吗?

合同
类中似乎没有实现用于此目的的方法。

不幸的是,找不到
合同的标准方法

但您可以使用这种方式(这种方式有一些限制):

    public class Test
    {
        public int x, y, z;//....

        public void Foo()
        {
            x = FooBody();
        }

        [Pure]
        private int FooBody()
        {
            int value = x;
            //work with value as x
            return value;
        }
    }