gpt4 book ai didi

c# - 接口(interface)契约混淆静态检查器

转载 作者:行者123 更新时间:2023-11-30 17:55:35 24 4
gpt4 key购买 nike

假设以下简单代码:

public class Foo // : IFoo
{
private string _field;

public string Property
{
get { return _field; }
}

private void SetField()
{
_field = " foo ";
}

private string Method()
{
SetField();
return Property.Trim();
}
}

静态检查器能够证明 PropertyMethod 使用它时不会为 null。

现在,我引入了一个接口(interface)和一个契约,静态检查器开始提示:“可能在空引用 'this.Property' 上调用方法。

这是错误还是我遗漏了什么?


界面代码如下:

public class Foo : IFoo
{
private string _field;

public string Property
{
get { return _field; }
}

private void SetField()
{
_field = " foo ";
}

private string Method()
{
SetField();
return Property.Trim();
}
}

[ContractClass(typeof(IFooContract))]
public interface IFoo
{
string Property { get; }
}

[ContractClassFor(typeof(IFoo))]
public abstract class IFooContract : IFoo
{
public string Property
{
get { throw new System.NotImplementedException(); }
}
}

我的设置是这样的:

我得到以下输出:

[...]
C:\{path}\CC2.cs(11,19): message : CodeContracts: Suggested ensures: Contract.Ensures(Contract.Result<System.String>() == this._field);
C:\{path}\CC2.cs(16,13): message : CodeContracts: Suggested ensures: Contract.Ensures(this._field != null);
C:\{path}\CC2.cs(21,13): message : CodeContracts: Suggested ensures: Contract.Ensures(Contract.Result<System.String>() != null);
C:\{path}\CC2.cs(21,13): message : CodeContracts: Suggested ensures: Contract.Ensures(this._field != null);
C:\{path}\CC2.cs(21,13): message : CodeContracts: Suggested ensures: Contract.Ensures(this.Property.Trim() != null);
C:\{path}\CC2.cs(21,13): message : CodeContracts: Suggested ensures: Contract.Ensures(Contract.Result<System.String>() == this.Property.Trim());
[...]
C:\{path}\CC3.cs(33,13): warning : CodeContracts: Possibly calling a method on a null reference 'this.Property'
[...]

我正在使用带有 .NET 4 的 Visual Studio 2010 Ultimate 作为目标框架。

最佳答案

不是一个答案,而是对这个问题的一些想法。这不是那种混淆代码契约的接口(interface)契约。我已经成功地用没有 ContractClass 接口(interface)的简单示例重现了这一点。只需将第二个示例更改为简单

//Foo's declaration

public interface IFoo
{
string Property { get; }
}

你会得到同样的错误。即使在 Property 字段上添加 Contract.Assume(_field != null); 也无法修复它(它将修复它,将此 Assume 添加到 SetField 方法)。我也没有设法用不变量抑制空引用异常警告。唯一可行的是一个非常丑陋的解决方案,您必须在其中为接口(interface)契约提供后置条件,并在 Property 字段中使用 assume 为 Code Contract 提供提示。完整代码如下所示

public class Foo  : IFoo
{
private string _field;

public string Property
{
get
{
Contract.Assume(_field != null);
return _field;
}
}

private void SetField()
{
_field = " foo ";

}

private string Method()
{
SetField();
return Property.Trim();
}
}

[ContractClass(typeof(IFooContract))]
public interface IFoo
{
string Property { get; }
}

[ContractClassFor(typeof(IFoo))]
public abstract class IFooContract : IFoo
{
public string Property
{
get
{
Contract.Ensures(Contract.Result<string>() != null);
throw new NotImplementedException();
}
}
}

编辑:由于 _field 可以为空,我建议使用这个方法体来给分析器提示,这样它就不会为空引用警告而烦恼。

private string Method()
{
SetField();
Contract.Assume(Property != null);
return Property.Trim();
}

附注正如 John Sonmez 在 pluralsight training regarding Code contracts 所说“静态分析是一项复杂、神秘的任务,如果不提示分析器调用 Assume 方法,几乎​​无法完成它”。

关于c# - 接口(interface)契约混淆静态检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14962341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com