I want my object invariant method to throw a specific exception. Does it make sense? Is it possible in C#?
For instance, I have the following code, including class A with invariant method and exception class E. For now class E is not participating in A...
class A {
int x = 0, y = 1;
[ContractInvariantMethod]
private void YisGreaterThanX() {
Contract.Invariant(x < y);
}
}
class E : Exception {
}
And what I need is the following. Like Contract.Requires it would be useful to have Contract.Invariant (or may be an attribute constructor, which accepts Exception-derived class).
class A {
int x = 0, y = 1;
[ContractInvariantMethod]
private void YisGreaterThanX() {
Contract.Invariant<E>(x < y);
}
}
class E : Exception {
}
Is it a good intention? May be my logic is wrong?