Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

limit or audit the use of an interface

Tags:

c#

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.

like image 525
Z.D. Avatar asked Jan 27 '26 13:01

Z.D.


2 Answers

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.)

like image 183
Jon Skeet Avatar answered Jan 29 '26 03:01

Jon Skeet


I'd place the interface in its own assembly (.csproj) and make only project B reference that assembly.

This way way you achieve, in my opinion, the best encapsulation while adhering to best practices.

like image 39
Luis Filipe Avatar answered Jan 29 '26 01:01

Luis Filipe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!