limit or audit the use of an interface

I have 3 assemblies:

  • A defines an interface
  • B references A and uses that interface
  • C also references A

how can I make sure C does not use that interface?

splitting assemblies - not an option.

Jon Skeet
people
quotationmark

You could make the interface internal, and use InternalsVisibleToAttribute to allow B access to the internal members of A (by adding the attribute to A). It's very coarse-grained though - you can't do it for individual members; it's all or nothing.

In general though, this sort of specific limitation just isn't part of the design of access control within .NET or C#.

Of course you could try splitting out the interface into a new assembly, and make that available to both A and B via InternalsVisibleTo - but you wouldn't want to do this too often. The platform isn't designed for this sort of thing. (It'll work, but it'll get harder to work with the more types you do this to.)

people

See more on this question at Stackoverflow