代码契约和接口继承的奇怪警告/错误

发布于 2024-12-03 21:10:22 字数 6161 浏览 1 评论 0原文

作为主题,我遇到了代码契约和接口的严重警告/错误。 这里是我的场景

主接口

[ContractClass(typeof(DTOInfoContract))]
public interface IDTOInfo
{
    int ID { get; }
    string Description { get; }        
    string LinkText { get; }
    string Title { get; }

    void DTOInit(int id, string title, string descr, string linkText);
}

辅助接口

[ContractClass(typeof(DTONewsContract))]
public interface IDTONews : IDTOInfo
{    
}

[ContractClass(typeof(DTOPromoContract))]
public interface IDTOPromo : IDTOInfo
{                
    string Photo { get; }

    void DTOPromoInit(int id, string title, string descr, string linkText, string Photo);

然后是我的合同

[ContractClassFor(typeof(IDTOInfo))]
public abstract class DTOInfoContract : IDTOInfo
{
    int IDTOInfo.ID
    {
        get { Contract.Ensures(Contract.Result<int>() > 0, "Returned value is out of Range"); return default(int); }
    }

    string IDTOInfo.Description
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.LinkText
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.Title
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    void IDTOInfo.DTOInit(int id, string title, string descr, string linkText)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "titolo has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }
}


}


[ContractClassFor(typeof(IDTONews))]
public abstract class DTONewsContract : IDTONews
{
    int IDTOInfo.ID
    {
        get { Contract.Ensures(Contract.Result<int>() > 0, "Returned value is out of Range"); return default(int); }
    }

    string IDTOInfo.Description
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.linkText
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.Title
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    void IDTOInfo.DTOInit(int id, string title, string descr, string linkText)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "title has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }
}


[ContractClassFor(typeof(IDTOPromo))]
public abstract class DTOPromoContract : IDTOPromo
{        
    string IDTOPromo.Photo
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }        

    void IDTOPromo.DTOPromoInit(int id, string title, string descr, string linkText, string photo)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "title has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");                  
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }

    int IDTOInfo.ID
    {
        get { Contract.Ensures(Contract.Result<int>() > 0, "Returned value is out of Range"); return default(int); }
    }

    string IDTOInfo.Description
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.LinkText
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.Title
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    void IDTOInfo.DTOInit(int id, string title, string descr, string linkText)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "title has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }
}

在这种情况下我只有一些警告,如下所示

warning CC1076: Contract class DTOPromoContract cannot define contract for method IDTOInfo.get_ID as its original definition is not in type IDTOPromo. Define the contract on type IDTOInfo instead.

但是,如果我尝试删除 IDTONews 和 IDTOPromo 合同类中的 IDTOInfo 方法和属性,则错误是,例如,以下

Error   40 'DTOPromoContract' does not implement the member of interface 'IDTOInfo.Description' 

是不是很奇怪的情况? 有没有一种解决方案可以将代码契约和接口与之前的场景进行最佳组合?

谢谢你!

as the topic subject, I've encountered strage Warnings/Erros with Code Contracts and Interface.
Here my scenario

Primary Interface

[ContractClass(typeof(DTOInfoContract))]
public interface IDTOInfo
{
    int ID { get; }
    string Description { get; }        
    string LinkText { get; }
    string Title { get; }

    void DTOInit(int id, string title, string descr, string linkText);
}

Secondary Interfaces

[ContractClass(typeof(DTONewsContract))]
public interface IDTONews : IDTOInfo
{    
}

[ContractClass(typeof(DTOPromoContract))]
public interface IDTOPromo : IDTOInfo
{                
    string Photo { get; }

    void DTOPromoInit(int id, string title, string descr, string linkText, string Photo);

And then my Contracts

[ContractClassFor(typeof(IDTOInfo))]
public abstract class DTOInfoContract : IDTOInfo
{
    int IDTOInfo.ID
    {
        get { Contract.Ensures(Contract.Result<int>() > 0, "Returned value is out of Range"); return default(int); }
    }

    string IDTOInfo.Description
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.LinkText
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.Title
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    void IDTOInfo.DTOInit(int id, string title, string descr, string linkText)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "titolo has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }
}


}


[ContractClassFor(typeof(IDTONews))]
public abstract class DTONewsContract : IDTONews
{
    int IDTOInfo.ID
    {
        get { Contract.Ensures(Contract.Result<int>() > 0, "Returned value is out of Range"); return default(int); }
    }

    string IDTOInfo.Description
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.linkText
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.Title
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    void IDTOInfo.DTOInit(int id, string title, string descr, string linkText)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "title has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }
}


[ContractClassFor(typeof(IDTOPromo))]
public abstract class DTOPromoContract : IDTOPromo
{        
    string IDTOPromo.Photo
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }        

    void IDTOPromo.DTOPromoInit(int id, string title, string descr, string linkText, string photo)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "title has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");                  
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }

    int IDTOInfo.ID
    {
        get { Contract.Ensures(Contract.Result<int>() > 0, "Returned value is out of Range"); return default(int); }
    }

    string IDTOInfo.Description
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.LinkText
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    string IDTOInfo.Title
    {
        get { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result<string>()), "Returned value is out of Range"); return default(string); }
    }

    void IDTOInfo.DTOInit(int id, string title, string descr, string linkText)
    {
        Contract.Requires<ArgumentOutOfRangeException>(id > 0, "id has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(title), "title has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(descr), "descr has no valid value");
        Contract.Requires<ArgumentOutOfRangeException>(!String.IsNullOrEmpty(linkText), "linkText has no valid value");
    }
}

With this situation I've only some Warnings like the following

warning CC1076: Contract class DTOPromoContract cannot define contract for method IDTOInfo.get_ID as its original definition is not in type IDTOPromo. Define the contract on type IDTOInfo instead.

But, if I try to delete the IDTOInfo method and properties in the IDTONews and IDTOPromo contract classes the errors was, for example, the following

Error   40 'DTOPromoContract' does not implement the member of interface 'IDTOInfo.Description' 

Is it a strange situation, isn't it?
Is there a solution to have the best combination of Code Contracts and Interface with the previous scenario?

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老子叫无熙 2024-12-10 21:10:22

契约类应该只指定它们所附加的接口的行为,而不是任何基接口。

因此,在DTOPromoContractDTONewsContract中,所有从IDTOInfo继承的方法都应该是abstract。 (所有接口契约类也应该是抽象的。)

Contract classes should only specify behaviour for the interface that they're attached to, and not any base interfaces.

So, in DTOPromoContract and DTONewsContract, all methods which are inherited from IDTOInfo should be abstract. (All interface contract classes should be abstract as well.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文