0

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

War
  • 8,539
  • 4
  • 46
  • 98
  • This kind of problem is easily solved using ReSharper. You can download a trial version at http://www.jetbrains.com/resharper/download. Good luck! – Roy Dictus Feb 16 '12 at 10:28
  • 1
    how does this help me solve my problem? could you provide a little more detail please? – War Feb 16 '12 at 10:58
  • Afaik ReSharper has no specific support for Contracts. I think @RoyDictus should elaborate a little. – H H Feb 16 '12 at 11:09
  • If a type or namespace cannot be found, hover over the unrecognized name and a ReSharper icon will appear. This will suggest what to do -- for instance, what assembly to reference. – Roy Dictus Feb 16 '12 at 11:58
  • hmmm ... no suggestions ... visual studio already seems to think that the reference is fine hense the blue highlight, it only underlines when i try to compile for some reason, odd. I think this might be a bug or something in .Net / the code contracts stuff ... (maybe) – War Feb 16 '12 at 12:07

1 Answers1

2

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

Manuel Fahndrich
  • 665
  • 4
  • 12
  • I emailed the code contract guys at micorsoft and after some complex explaining I found that this was the problem ... it's interesting to note that code contracts don't currently work (in inheritence terms) the way that the rest of the framework does e.g. ... contract : contractBase, Icontract ... perfectly valid in normal .net space but not in code contracts. full details can be found over at: http://social.msdn.microsoft.com/Forums/en-US/codecontracts/thread/9cf0acd9-add0-41d3-8e77-20928ddb000d – War Feb 17 '12 at 16:56
  • @Wardy You may be interested to know that MaF is actually one of the code contract guys – Lukazoid Apr 24 '13 at 20:43