+ 2

What is the best way to simulate multiple inheritance in C#?

I know that by default C# does not allow multiple inheritance. But is it possible to do it using Interfaces, Reflection, Dynamic, Roslin, etc?

22nd Aug 2019, 4:53 PM
Leynier Gutiérrez González
Leynier Gutiérrez González - avatar
5 Respuestas
+ 6
You can inherit only one class in c#. In addition to that you can inherit multiple interfaces but be careful not to violate the SOLID principles. If you inherit intefaces you must manually write the methods or properties etc. You can also create an instance of a Unit of Work if you created one in your code somewhere. That helps mostly if you have mostly the same actions on a lot of code like CRUD operations. Other than that I too am open to more knowledge that these.
22nd Aug 2019, 6:23 PM
Tugay Mandal
+ 4
Interfaces
23rd Aug 2019, 2:13 AM
Sonic
Sonic - avatar
+ 3
Tugay Mandal the problem with this way is that when an instance of the Multiple class is passed to a method that expects an instance of the Driver class, because of the polymorphism, the method of the Multiple class must be executed if a true multiple inheritance is to be obtained , but this does not happen, the method that is executed is that of the Driver class. https://code.sololearn.com/cTgS91pc4eYQ/#
22nd Aug 2019, 7:21 PM
Leynier Gutiérrez González
Leynier Gutiérrez González - avatar
+ 3
You can use this factory pattern for similar code. https://code.sololearn.com/cS48XRijh034/?ref=app
24th Aug 2019, 6:01 PM
Tugay Mandal
+ 2
I thinl you can use one Iwork interface and 3 different classes with Working methods and make the multiple class a generic like Multiple<Ttype>(etc) and then call the classes and their corresponding methods.
23rd Aug 2019, 7:47 AM
Tugay Mandal