代码契约和接口继承的奇怪警告/错误
作为主题,我遇到了代码契约和接口的严重警告/错误。 这里是我的场景
主接口
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
契约类应该只指定它们所附加的接口的行为,而不是任何基接口。
因此,在
DTOPromoContract
和DTONewsContract
中,所有从IDTOInfo
继承的方法都应该是abstract
。 (所有接口契约类也应该是抽象的。)Contract classes should only specify behaviour for the interface that they're attached to, and not any base interfaces.
So, in
DTOPromoContract
andDTONewsContract
, all methods which are inherited fromIDTOInfo
should beabstract
. (All interface contract classes should beabstract
as well.)