Strange Warnings/Erros with Code Contracts and Interface inheritance
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 v开发者_如何学编程alid 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!
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.)
精彩评论