Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Following our comments exchange, you problem now is seen in a different light than the original post. You can't have directly what multiple inheritance would have given you. But there are various ways you can get very close. </p> <p>Variant 1: Trick your special classes that need <code>MyFunction()</code> into receiving it as an extension method. To accomplish this, you need a "differentiator"; in my example that is the ILwA interface. Note that it has to define another commonality, not <code>MyFunction()</code>. A getter (or a setter) will do. Limitation here is that MyFunction doesn't have access to private members. </p> <pre><code>//instead of the lightweight interface (but use interface if it makes more sense) abstract class LwBase abstract class HwBase : LwBase public interface IADiff { int APropTrick { get; } } // lightweight class definitions class LwA : LwBase, IADiff class LwB : LwBase // full class definitions class FullA : HwBase, IADiff class FullB : HwBase public static class AExtensions { public static void MyFunction(this IADiff o) { // impl. } } </code></pre> <p>Variant 2: use a common class that provides implementation of <code>MyFunction()</code> and "redirect" to it:</p> <pre><code>abstract class LwBase abstract class HwBase : LwBase public interface IADiff { int MyFunction(); } public class AConcr : IADiff //implemenst Myfunction() // lightweight class definitions class LwA : LwBase, IADiff { private AConcr a = new AConcr(); public int MyFunction() { return a.MyFunction(); } } class LwB : LwBase // full class definitions class FullA : HwBase, IADiff { private AConcr a = new AConcr(); public int MyFunction() { return a.MyFunction(); } } class FullB : HwBase </code></pre> <p>Variant 3: I stumbled on <a href="http://www.codeproject.com/Articles/10072/Simulated-Multiple-Inheritance-Pattern-for-C" rel="nofollow noreferrer">this article</a> as I was researching your question. I can't quickly say if it's a viable option, but it certainly provides ideas.</p> <p>Credits: I am heavily borrowing from <a href="https://stackoverflow.com/questions/2515886/design-pattern-to-use-instead-of-multiple-inheritance">this SO thread</a>, as well as using <a href="http://www.dofactory.com/Patterns/Patterns.aspx" rel="nofollow noreferrer">this nice site</a> as a reference.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload