2010-05-23 47 views
1

以下代碼使ccrewrite炸燬!想法? 順便說一句,如果你註釋掉ActualClass,ccrewrite成功...CodeContracts:ccrewrite失敗,對象引用未設置爲對象的實例

[ContractClass(typeof(TestContracts))] 
    interface ITestInterface 
    { 
     bool IsStarted { get; set; } 
     void Begin(); 
    } 

    class ActualClass : ITestInterface 
    { 
     public bool IsStarted { get; set; } 
     public void Begin() 
     { 
      this.IsStarted = true; 
     } 
    } 

    [ContractClassFor(typeof(ITestInterface))] 
    class TestContracts : ITestInterface 
    { 
     ITestInterface Current { get; set; } 

     private TestContracts() 
     { 
      Current = this; 
     } 

     #region ITestInterface Members 

     bool ITestInterface.IsStarted 
     { 
      get; set; 
     } 

     void ITestInterface.Begin() 
     { 
      Contract.Requires(!Current.IsStarted); 
      Contract.Ensures(Current.IsStarted); 
     } 

提前感謝!

回答

1

好,服務我的權利不讀喬恩斯基特不夠好;)關於重寫是如何發生的合同並把他們在您的實際類的有點...

[ContractClassFor(typeof(ITestInterface))] 
    class TestContracts : ITestInterface 
    { 

     private TestContracts() 
     { 
     } 

     #region ITestInterface Members 

     bool ITestInterface.IsStarted 
     { 
      get; set; 
     } 

     void ITestInterface.Begin() 
     { 
      ITestInterface iface = this; 
      Contract.Requires(!iface.IsStarted); 
      Contract.Ensures(iface.IsStarted); 
     } 
    } 

http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/853227bf-56e6-427b-8e9e-162c129e87ce/

相關問題