【问题标题】:Is this a bug in the Code Contracts rewriter?这是代码合同重写器中的错误吗?
【发布时间】:2010-08-23 01:50:24
【问题描述】:

我正在试验 .NET 代码合同。以下代码在运行时合约检查关闭时运行良好,但在运行时合约检查打开时失败

using System.Collections.Generic;
using System.Diagnostics.Contracts;

namespace ConsoleApplication1
{
    public class Item<T> where T : class { }
    public class FooItem : Item<FooItem> { }

    [ContractClass(typeof(ITaskContract<>))]
    public interface ITask<T> where T : Item<T>
    {
        void Execute(IEnumerable<T> items);
    }

    [ContractClassFor(typeof(ITask<>))]
    internal abstract class ITaskContract<T> : ITask<T> where T : Item<T>
    {
        void ITask<T>.Execute(IEnumerable<T> items)
        {
            Contract.Requires(items != null);
            Contract.Requires(Contract.ForAll(items, x => x != null));
        }
    }

    public class FooTask : ITask<FooItem>
    {
        public void Execute(IEnumerable<FooItem> items) { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            new FooTask();
        }
    }
}

我在运行此代码时遇到的错误不是违反合同。相反,重写器似乎正在以某种方式生成损坏的二进制文件:

未处理的异常:System.BadImageFormatException:尝试加载格式不正确的程序。 (来自 HRESULT 的异常:0x8007000B) 在 ConsoleApplication1.Program.Main(String[] args)

如果我删除以下行,错误就会消失:

Contract.Requires(Contract.ForAll(items, x => x != null));

我做错了什么,或者这是二进制重写器中的错误?我该怎么办?

【问题讨论】:

  • 我在 32 位 x86 机器上运行它。我尝试将构建配置中的平台目标设置为“x86”和“任何 CPU”,但无论哪种方式我都得到相同的结果。我没有尝试在 x64 机器上运行它。
  • 我看到了同样的结果。 Win7 Prof 64bit + VS 2010 Ultimate,代码合同 1.4.30707.2。它仍处于开发阶段,因此会有一些错误。
  • 我建议你在 CC 论坛上报告这个问题,这里:social.msdn.microsoft.com/Forums/en/codecontracts/threads

标签: c# .net .net-4.0 code-contracts


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 2021-08-09
相关资源
最近更新 更多