代码契约继承
发现代码契约有点奇怪,我想知道是否有人知道原因...
好吧,现在是一些代码示例的时间了:
程序集 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
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”命名空间和缺少的类所在的命名空间在。 该类被声明为公共类并显示在反射器中,那么为什么它找不到它呢?
程序集之间的代码契约继承是否存在问题?
编辑:
只是一个想法,从另一个程序集继承契约基类是否会出现问题......这个东西在编译时不会做一些疯狂的二进制注入事情吗?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
派生类 (ICampaignServiceCodeContract) 的契约类不应派生自除它所注释的类(在本例中为 ICampaignService)之外的任何内容。
您可以保留从基接口/类继承的所有方法未实现(使用 VS 生成的默认主体),并仅在特定于此类/接口的方法中编写契约。
-MaF
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