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?
Interfaces provide an alternative to multiple inheritance. Java programming language does not support multiple inheritance. But interfaces provide a good solution.
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.
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.
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.
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:
together with multiple interfaces or one concrete derivation and one interface. To simulate multiple inheritance you:
This limits the amount of code in your implementing class to just one forwarding call per interface method or property.
You should be able to do that with the DynamicObject
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With