问题
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