CodeContracts falsely denotes a missing precondition already in the base constructor
Let's say I have the following class hierarchy:
public class FooBase
{
private readonly object _obj;
protected FooBase(object obj)
{
Contract.Requires(obj != null);
_obj = obj;
}
}
public class Foo : FooBase
{
public Foo(object obj) : base(obj)
{
}
}
When I compile I get the following CodeContracts error for Foo
:
Error 12 CodeContracts: Missing precondition in an externally visible method. Consider adding Contract.Requires(obj != null); for parameter validation
Is there a way to get CodeContracts to recognize that validation is already happening in the base class?
+3
source to share
1 answer
Unfortunately no. Your Foo calls FooBase (obj) unnecessarily.
public class FooBase
{
private readonly object _obj;
protected FooBase(object obj)
{
Contract.Requires(obj != null);
_obj = obj;
}
}
public class Foo : FooBase
{
public Foo(object obj) : base(obj)
{
Contract.Requires(obj != null);
}
}
will be the only way to fix this problem.
0
source to share