Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c# - Building with Code Contracts?

I have the following method:

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

With Code Contracts we can write it like this:

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

The second method is shorter and simpler. The problem that I have is that when you build it, in runtime it does not throw the exception, it shows this:

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.

Unless with VS you download the CodeContracts for .net from here.

And then you check the "Runtime check" in the project, so that when you build it in runtime, the exception is thrown.

Our app is build with Jenkins with PowerShell scripts. Is there any way to check in runtime and throw the exception, with a simple command or attribute, or something easy?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By changing following project properties I could eliminate getting this exception while running.

Right click on project -> Properties -> Code Contract (Tab) change the assembley mode to "Standard Contract Requires" also select checkbox - Perform Runtime contract checking


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...