How to hide the whole class?

Let's imagine:

// assembly 01 abstract class Foo { abstract void Bar(); } class AdvancedFoo : Foo { override void Bar() { base.Foo(); ... } } sealed class SuperiorFoo : AdvancedFoo { override sealed void Bar() { base.Foo(); } } 

And then I want to reference this assembly 01 from my 02 asm and replace the intermediate base class (and only it) so that the SuperiorFoo instances act as if they were leaving the AdvancedFoo assembly 02 .

 // assembly 02 class AdvancedFoo : Foo { override void Bar() { base.Foo(); ... } } 

Is there a way to do this without rewriting the SuperiorFoo class declaration if assembly 02 ?

 var supFoo = new SuperiorFoo(); supFoo.Bar(); // This one should call assembly 02 Bar(); 
+1
source share
2 answers

Not like you do not, but you can use composition rather than inheritance to solve your problem:

 interface IFoo { abstract void Bar(); } class AdvancedFoo : IFoo { override void Bar() { base.Foo(); ... } } sealed class SuperiorFoo : IFoo { private IFoo _fooImplementer; public SuperiorFoo() { _fooImplemnter = new AbstractFoo(); } public SuperiorFoo(IFoo fooImplementer) { _fooImplementer = fooImplementer; } void Bar() { _fooImplementer.Foo(); } } 

To call from the second assembly, you must use the second constructor by passing an instance of your new advancedfoo class.

+2
source

Source: https://habr.com/ru/post/952183/


All Articles