Code Contracts: Invariants in abstract class

淺唱寂寞╮ 提交于 2019-12-05 19:18:14

问题


I have encountered a problem while using Invariants with Code Contracts. I want to define an Invariant within my abstract class but it is simply ignored. The code below shows my interface and the abstract class.

[ContractClass(typeof(IPointContract))]
interface IPoint
{
    int X { get; }
    int Y { get; }
}

[ContractClassFor(typeof(IPoint))]
abstract class IPointContract : IPoint
{

    public int X
    {
        get { return 0; }

    }

    public int Y
    {
        get { return 0; }
    }

    [ContractInvariantMethod]
    private void PointInvariant()
    {
        Contract.Invariant(X > Y);
    }
}

Afterwards, I implement this interface within my Point class and create an object from it. This should at least fail during runtime.

class Point : IPoint
{
    public Point(int X, int Y)
    {
        this._x = X;
        this._y = Y;
    }

    private int _x;
    public int X
    {
        get { return _x; }
    }

    private int _y;
    public int Y
    {
        get { return _y; }
    }
}

class Program
{
    static void Main(string[] args)
    {
        Point p = new Point(1, 2);
    }
}

When I move the Invariant to the Point-Class, it works fine. All other pre- or post conditions are working fine too.

Is it not possible to have Invariants within an abstract class or am I doing it wrong?


回答1:


Invariants are not supported on interfaces. (Your question title is "Invariants in abstract class" but the crux of the problem is with the interface.)

My guess is that this is because invariants require state but interfaces don't have state. I'm sure the code contracts team can work around this and I wish they would because this would be a great feature to have.

To work around this limitation you could:

  • Add the invariant method to the derived classes (class Point, etc.).
  • Or, add setters to the abstract class properties and implement the contract logic in the setters.


来源:https://stackoverflow.com/questions/29123695/code-contracts-invariants-in-abstract-class

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