Philip C
Philip C

Reputation: 1847

CodeContracts static checking says the members of an internal class of another assembly are not visible enough, despite using InternalVisibleTo

I'm writing a program in Visual Studio 2012, and I have a pair of classes in two separate projects:

ProjectA:

namespace Test
{
  internal class A
  {
    private A(B b)
    {
      Contract.Requires(b.X != null);
    }
  }
}

ProjectB:

namespace Test
{
  internal class B
  {
    public string X;
  }
}

In AssemblyInfo.cs in ProjectB, I also have:

[assembly: InternalsVisibleTo("ProjectA")]

This compiles just fine until I turn on CodeContract static analysis. At this point, I get an error from ccrewrite: Member 'Test.B.X' has less visibility than the enclosing method Test.A.#ctor(Test.B).

The only thing that I can think might be causing the problem is that CodeContracts doesn't know that ProjectA can see ProjectB's internals, and thus believes that class B's visibility is essentially none with this context. Although if I change the assertion to b != null, it seems to be fine, so maybe this argument doesn't hold water.

Can anyone confirm this assertion, or give me a correct explanation, and short of removing the Requires, turning off CodeContracts or changing the visibility of B, is there a way around this error?

Upvotes: 0

Views: 360

Answers (1)

Peter
Peter

Reputation: 27944

Code contract does not know the meaning of the attribute:

[assembly: InternalsVisibleTo("ProjectA")]

So it does not know that the interals are visible to projectA. InternalsVisibleToAttribute is designed to ease unittesting.

You should think about why you are seprating the two classes in different assamblies when they need to know each other. Maybe you can solve it with a interface, and some sort of factory.

Upvotes: 1

Related Questions