This is a bit of a general question. Say I have a class X in A.dll and B.dll references A.dll and C.dll references B.dll.
Now, I need to use X in C.dll(the consumer code). So what I would want to do is to make a wrapper class X1 in B.dll and use that in the consumer code. So here’s the question? How to make X1 such that it is an (almost)perfect wrapper of X?
Thanks.
.NET requires you to have a direct reference to A.dll if you use X or a class that inherits from X. Assuming that you do not want the consumer C.dll to reference A.dll directly, there are two ways. If you want to totally hide X, you should create a true wrapper (see example below). The alternative is to use late binding (not recommended, but C gets access to X with only an indirect assembly reference).
Using a statically typed wrapper (preferred method)
Assuming you want a wrapper (and not just inheriting X), I suggest you simply create a manual wrapper. I.e. create a class X1 that has a private variable of type X. Implement all public functions and properties of X by calling the corresponding member in X. This will work if all you need is a wrapper for X and nothing else. In this way, you can hide X from B (I assume that this is what you want).
A.dll
B.dll
Using late binding (no manual wrapper needed)
Not technically a wrapper, but if you want to avoid A.dll as a direct reference from C.dll
A third alternative is not to wrap X, but to make C.dll use X as a unknown object. This method makes use of call sites and the ‘dynamic’ object in C#. This allows B.dll to return the true X without having to explicitly reference it at compile time from C.dll (assuming that you have a good reason not to do that). Microsoft creates so called ‘call sites’ for each property/function call from C.dll such that the overhead is very low. This makes X perfect as it is really X. The good thing about the dynamic method is that the wrapper will following any changes to X. The bad thing is that C.dll will have runtime binding (no compilation errors).