gpt4 book ai didi

c# - 使用代码契约构建?

转载 作者:可可西里 更新时间:2023-11-01 07:47:15 24 4
gpt4 key购买 nike

我有以下方法:

private void DoSomething(CoolClass coolClass)
{
if (coolClass == null)
{
throw new ArgumentNullException("coolClass");
}
coolClass.Name = "Pepe";
}

有了代码契约,我们可以这样写:

private void DoSomething(CoolClass coolClass)
{
Contract.Requires<ArgumentNullException>(coolClass != null, "IS NULLL!");
coolClass.Name = "Pepe";
}

第二种方法更短更简单。我遇到的问题是,当您构建它时,在运行时它不会抛出异常,它显示如下:

Description: An assembly (probably "CodeContractsTest") must be rewritten using the code contracts binary rewriter (CCRewrite) because it is calling Contract.Requires and the CONTRACTS_FULL symbol is defined. Remove any explicit definitions of the CONTRACTS_FULL symbol from your project and rebuild. CCRewrite can be downloaded from http://go.microsoft.com/fwlink/?LinkID=169180. After the rewriter is installed, it can be enabled in Visual Studio from the project's Properties page on the Code Contracts pane. Ensure that "Perform Runtime Contract Checking" is enabled, which will define CONTRACTS_FULL.

除非使用 VS,否则您从 here 下载 .net 的 CodeContracts| .

然后你勾选项目中的“Runtime check”,这样当你在运行时构建它时,就会抛出异常。

我们的应用程序是使用 Jenkins 和 PowerShell 脚本构建的。有没有什么方法可以使用简单的命令或属性或其他简单的方法来检查运行时并抛出异常?

最佳答案

通过更改以下项目属性,我可以避免在运行时出现此异常。

右键单击项目 -> Properties -> Code Contract (Tab)将汇编模式更改为“标准契约(Contract)要求”,同时选中复选框 - 执行运行时契约(Contract)检查

关于c# - 使用代码契约构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18793558/

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