代码合同继承 [英] Code contract inheritance

查看:95
本文介绍了代码合同继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现代码合同有点奇怪,我想知道是否有人知道原因...

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:

程序集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 { ... }

汇编2:

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

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

现在这是我的问题...
在最后一行代码中,编译器运行良好,直到我实际编译代码,然后突出显示 IServiceCodeContract< Campaign>。蓝线表示找不到实际错误的类型:

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?)

我有一个引用从程序集2到程序集1,我同时导入了 System.Diagnostics.Contracts命名空间和缺少的类所在的命名空间。
该类被声明为public并显示在反射器中,为什么?

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?

编辑:

只是一个想法,从另一个程序集中继承一个合约基类会不会有问题...这不是在编译时做一些疯狂的二进制注入吗?

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?

也发布在这里: http: //forums.asp.net/t/1770324.aspx/1

推荐答案

您的派生类(ICampaignServiceCodeContract)的合同类不应从任何东西中派生,但要注释的类(在本例中为ICampaignService) )。

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

您可以保留从基本接口/类继承的所有方法未实现(使用VS生成的默认主体),并且仅在特定于

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

这篇关于代码合同继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆