问题
I've got Code Contracts working fine from inside Visual Studio 2010, but I can't get ccrewrite.exe
to do anything useful from the command line. Here's a sample app:
using System.Diagnostics.Contracts;
public class Dummy
{
public static void Main(string[] args)
{
Contract.Requires(args.Length > 0);
}
}
I then compile the code and run ccrewrite.exe on it:
> csc /debug+ /D:CONTRACTS_FULL Dummy.cs
> ccrewrite /o:RewrittenDummy.exe Dummy.exe
elapsed time: 61ms
There's no RewrittenDummy.exe
file afterwards.
I've tried loads of options, but nothing's making any difference. A few things I've noticed:
- It's definitely loading Dummy.exe, because if I specify a non-existent file, it dies
- Dummy.exe definitely contains references to Contract - if I run it with no arguments, it fails appropriately (but the error message hasn't been filled in as I'd expect if it had been rewritten)
- Using postconditions and invariants makes no difference
I've tried turning warnings and verbosity up, and that doesn't help at all What am I doing wrong?
(Also asked as a question in the Code Contracts forum. I'll add any relevant answers here myself.)
回答1:
Okay, this has been answered in the MSDN forum. For once, it wasn't really me being entirely stupid - it's something that could really do with a warning.
Read the forum post for full details, but the basic problem is that ccrewrite
couldn't find the contract classes: it was looking in the .NET 3.5 CLR version of mscorlib
instead of the .NET 4.0 one.
This can be fixed by explicitly listing the path to the relevant assembly:
> ccrewrite /o:rewrittendummy.exe dummy.exe
/libpaths:%SystemRoot%\Microsoft.Net\Framework\v4.0.20506
来源:https://stackoverflow.com/questions/1209166/why-is-ccrewrite-exe-not-doing-anything-from-the-command-line