Code contract inheritance

随声附和 提交于 2019-12-12 02:29:59

问题


Found a bit of an oddity with code contracts and i was wondering if anyone knew the cause ...

ok so time for some code samples:

Assembly 1:

[ContractClass(typeof(IServiceCodeContract<>))]
public interface IService<T> where T : class { ... }

[ContractClassFor(typeof(IService<>))]
public abstract class IServiceCodeContract<T> : IService<T> where T : class { ... }

public abstract class ServiceBase<T> : IService<T> where T : class { ... }

Assembly 2:

[ContractClass(typeof(ICampaignServiceCodeContract))]
public class CampaignService : ServiceBase<Campaign>, ICampaignService { ... }

[ContractClassFor(typeof(ICampaignService))]
public abstract class ICampaignServiceCodeContract : IServiceCodeContract<Campaign>, ICampaignService { ... }

Now here's my problem ... On that last line of code the compiler is fine until i actually compile the code then it highlights "IServiceCodeContract<Campaign>" with a blue line saying that it couldn't find the type the actual error reads:

The type or namespace name 'IServiceCodeContract' could not be found (are you missing a using directive or an assembly reference?)

i have a reference from assembly 2 to assembly 1 and i have imported both the "System.Diagnostics.Contracts" namespace and the namespace that the missing class lives in. The class is declared as public and shows up within reflector ok so why wouldn't it find it?

Is there some issue with code contract inheritance between assemblies or something?

EDIT:

Just a thought, could there be a problem inheriting a contract base class from another assembly ... doesn't this stuff do some crazy binary injection thing at compile time?

also posted here: http://forums.asp.net/t/1770324.aspx/1


回答1:


Your contract class for the derived class (ICampaignServiceCodeContract) should NOT derive from anything but the class it is annotating (in this case the ICampaignService).

You can leave all the methods inherited from base interfaces/classes unimplemented (using the default body generated by VS) and write contracts only in the methods that are specific to this class/interface.

-MaF



来源:https://stackoverflow.com/questions/9309236/code-contract-inheritance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!