代码收缩静态检查器:可能在空引用上调用方法 - 为什么?

时间:2014-10-02 12:32:06

标签: c# .net code-contracts

我有以下课程:

public class AndSpecification<TEntity> : ISpecification<TEntity>
{
    protected ISpecification<TEntity> First { get; private set; }
    protected ISpecification<TEntity> Second { get; private set; }

    public bool IsSatisfiedBy(TEntity entity)
    {
        return First.IsSatisfiedBy(entity) && Second.IsSatisfiedBy(entity);
    }

    public AndSpecification(ISpecification<TEntity> first, ISpecification<TEntity> second)
    {
        Contract.Requires(first != null);
        Contract.Requires(second != null);
        Contract.Ensures(First != null);
        Contract.Ensures(Second != null);

        First = first;
        Second = second;
    }
}

请注意,我使用Contract.Ensures()来确保在调用构造函数后,FirstSecond永远不会为空。但是,代码合同在IsSatisfiedBy()方法实施中向我发出警告:CodeContracts: Possibly calling a method on a null reference 'this.First'(对于Second也是如此)。无法弄清楚我或静态检查器有什么问题?

1 个答案:

答案 0 :(得分:0)

看来你缺少一些合同和对象不变量。我把你的代码放在上面并对它进行了Code Contracts静态分析,并得到了你所得到的。然后我通过代码来完成满足静态分析器的实现。

[ContractClass(typeof(ISpecificationContracts<>))]
public interface ISpecification<TEntity> 
    where TEntity : class
{
    bool IsSatisfiedBy(TEntity entity);
}

[ContractClassFor(typeof(ISpecification<>))]
abstract class ISpecificationContracts<TEntity> 
    : ISpecification<TEntity> where TEntity : class
{
    public bool IsSatisfiedBy(TEntity entity)
    {
        Contract.Requires(entity != null);
        throw new NotImplementedException();
    }
}

public class AndSpecification<TEntity> 
    : ISpecification<TEntity> where TEntity : class
{
    private readonly ISpecification<TEntity> _first;
    private readonly ISpecification<TEntity> _second;

    protected ISpecification<TEntity> First {
        get
        {
            Contract.Ensures(Contract.Result<ISpecification<TEntity>>() != null);
            return _first;
        }
    }
    protected ISpecification<TEntity> Second
    {
        get
        {
            Contract.Ensures(Contract.Result<ISpecification<TEntity>>() != null);
            return _second;
        }
    }

    public bool IsSatisfiedBy(TEntity entity)
    {
        return First.IsSatisfiedBy(entity) && Second.IsSatisfiedBy(entity);
    }

    public AndSpecification(ISpecification<TEntity> first,
                            ISpecification<TEntity> second)
    {
        Contract.Requires(first != null);
        Contract.Requires(second != null);
        Contract.Ensures(_first == first);
        Contract.Ensures(_second == second);

        _first = first;
        _second = second;
    }

    [ContractInvariantMethod]
    [System.Diagnostics.CodeAnalysis.SuppressMessage(
        "Microsoft.Performance", 
        "CA1822:MarkMembersAsStatic", 
        Justification = "Required for code contracts.")]
    private void ObjectInvariant()
    {
        Contract.Invariant(_first != null);
        Contract.Invariant(_second != null);
    }

}