Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I somehow mimic multiple inheritance using dynamic objects?

I have just been refactoring nearly all classes in my Services layer to inherit from a ServiceBase, to reduce duplication in initializing data access and other aspects identical to nearly all services, but I was stopped in my tracks when I reached my RoleService, as it has to inherit from RoleProvider so that I can configure it as my web site's 'official' role provider.

Now it is a bit late at night, and the caffeine is on form, but I was wondering if there was any way to use a dynamic object in place of a derived object, and add all the members of the base object to the 'derived' object, at runtime, instead of compile time.

Is this even remotely possible?

like image 449
ProfK Avatar asked Apr 19 '11 21:04

ProfK


People also ask

What is alternative for multiple inheritance?

Interfaces provide an alternative to multiple inheritance. Java programming language does not support multiple inheritance. But interfaces provide a good solution.

Is it possible to inherit from multiple classes at the same time?

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited.

Why does .NET not support multiple inheritance?

NET and Java designers did not allow multiple inheritance because they reasoned that adding MI added too much complexity to the languages while providing too little benefit.

Why multiple inheritance is not possible in C sharp?

C# compiler is designed not to support multiple inheritence because it causes ambiguity of methods from different base class. This is Cause by diamond Shape problems of two classes If two classes B and C inherit from A, and class D inherits from both B and C.


2 Answers

No, DynamicObject does not allow you derive from two concrete classes, which is what multiple inheritance is, and which C# does not support. The problem you face is the same either way, dynamic or static. If you have Base1 and Base2 that are unrelated to each other, then as soon as you derive Derived from Base1, there is no way that that Derived is Base2 can ever be true. Instead you can settle for Derived is IBase2.

I recommend that you use the:

  • Bridge Pattern

together with multiple interfaces or one concrete derivation and one interface. To simulate multiple inheritance you:

  • create an interface instead of a base
  • implement the interface instead of deriving from the base
  • create an implementor class that does the work you wanted to do in the base
  • "bridge" to the implementor by forwarding all the interface calls to an instance of the implementor

This limits the amount of code in your implementing class to just one forwarding call per interface method or property.

like image 168
Rick Sladkey Avatar answered Oct 03 '22 00:10

Rick Sladkey


You should be able to do that with the DynamicObject class.

like image 20
Mark Cidade Avatar answered Oct 03 '22 01:10

Mark Cidade