以下代码使ccrewrite爆炸!想法? 顺便说一句,如果你注释掉ActualClass,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);
}
提前致谢!
答案 0 :(得分:1)
好吧,为了不让Jon Skeet读得很好,这对我来说是正确的;)关于重写者如何接受合同并将它们放入实际班级的一点......
[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);
}
}