coffee_machine
coffee_machine

Reputation: 1223

C# Use a class that implements an interface without adding a reference to the assembly that defines the interface

I have 3 assemblies written in C#, namely A.exe, B.dll, C.dll

My question: is it possible to compile B without referencing C.dll ? I do not use it and I want to prevent a developer from using it (i.e. typing "IfaceC" in the B's code accidentally). However A.exe still needs it.

EDIT

Given my archi, A can reference C but not B, C cannot reference nor A neither B, B can reference A but not (if possible) C

Upvotes: 5

Views: 3162

Answers (2)

sehe
sehe

Reputation: 393174

The only other way evades your question and involves Duck Typing.

C#'s dynamic type keyword is very handy in this respect. It has the drawback of no longer having static type checking during compilation.

In effect, using dynamic you can pass in any object of (unknown) type, much like you pass object instances. The compiler let's you use the object by calling methods/properties and whatnot, as if the type was known.

Code will be emitted that looks up the relevant properties at runtime. This means that at runtime the metadata will be consulted (and the common assembly is still being loaded). The net gain is only that the 'third' assembly is not referenced, and need not be present at compilation or program loading time.


Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1501163

No. You need to have a reference to any assemblies containing base types or interfaces implemented by any types you use.

Upvotes: 4

Related Questions