CodeContracts: فشل ccrewrite مع مرجع الكائن لم يتم تعيينه على مثيل لكائن

StackOverflow https://stackoverflow.com/questions/2890524

سؤال

الرمز أدناه يجعل ccrewrite تفجير! أفكار؟ راجع للشغل ، إذا قمت بتعليق على 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);
        }

شكرا مقدما!

هل كانت مفيدة؟

المحلول

حسنًا ، يخدمني بشكل صحيح لعدم قراءة Jon Skeet جيدًا بما فيه الكفاية ؛) الشيء حول كيفية قيام Repriter بعقود ويضعها في صفك الفعلي ...

   [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/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top