ccrewrite失败,并且“对象引用未设置为对象实例”。 [英] ccrewrite fails with "Object reference not set to an instance of object"

查看:79
本文介绍了ccrewrite失败,并且“对象引用未设置为对象实例”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当编译下面的代码'ccrewrite'失败并且"对象引用没有设置为对象的实例"时:

When compiling below code 'ccrewrite' fails with "Object reference not set to an instance of object":


  public class FooItem
  {
    public string Type { get; set; }
  }
 
  [ContractClass(typeof(FooContracts))]
  public interface IFoo
  {
    void Bar(IEnumerable<FooItem> items);    
  }

  [ContractClassFor(typeof(IFoo))]
  internal abstract class FooContracts : IFoo
  {    
    public void Bar(IEnumerable<FooItem> items)
    {
      Contract.Requires(items != null);
      Contract.Requires(Contract.ForAll(items, _ => _ != null));
      throw new NotImplementedException();
    }      
  }

  public class ActualFoo : IFoo
  {
    public void Bar(IEnumerable<FooItem> items)
    {
      var groups = items.GroupBy(_ => _.Type);
      throw new NotImplementedException();
    }     
  }  

推荐答案

这通常不是我的经验要求。我怀疑在这种情况下的要求与您使用IEnumerable< FooItem>有某种关系。作为Bar方法的类型参数。有趣的是,以下代码没有此要求:

This is not usually a requirement in my experience. I suspect the requirement in this case is somehow related to your use of IEnumerable<FooItem> as type parameter to Bar method. Interestingly, the following code doesn't have this requirement:


  public class FooItem
  {
    public string Type { get; set; }
  }

  [ContractClass(typeof(FooContracts<>))]
  public interface IFoo<T> where T:FooItem
  {
    void Bar(IEnumerable<T> items);
  }

  [ContractClassFor(typeof(IFoo<>))]
  internal abstract class FooContracts<T> : IFoo<T> where T:FooItem
  {
    public void Bar(IEnumerable<T> items)
    {
      Contract.Requires(items != null);
      Contract.Requires(Contract.ForAll(items, _ => _ != null));
      throw new NotImplementedException();
    }
  }

  public class ActualFoo : IFoo<FooItem>
  {
    public void Bar(IEnumerable<FooItem> items)
    {
      var groups = items.GroupBy(_ => _.Type);
      throw new NotImplementedException();
    }
  } 


这篇关于ccrewrite失败,并且“对象引用未设置为对象实例”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆