Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to "wrap" implementation in C#
    primarykey
    data
    text
    <p>I have these classes in C# (.NET Framework 3.5) described below:</p> <pre><code>public class Base { public int State {get; set;} public virtual int Method1(){} public virtual string Method2(){} ... public virtual void Method10(){} } public class B: Base { // some implementation } public class Proxy: Base { private B _b; public Proxy(B b) { _b = b; } public override int Method1() { if (State == Running) return _b.Method1(); else return base.Method1(); } public override string Method2() { if (State == Running) return _b.Method2(); else return base.Method2(); } public override void Method10() { if (State == Running) _b.Method10(); else base.Method10(); } } </code></pre> <p><strong>I want to get something this</strong>:</p> <pre><code>public Base GetStateDependentImplementation() { if (State == Running) // may be some other rule return _b; else return base; // compile error } </code></pre> <p><strong>and my Proxy's implementation will be</strong>: </p> <pre><code>public class Proxy: Base { ... public override int Method1() { return GetStateDependentImplementation().Method1(); } public override string Method2() { return GetStateDependentImplementation().Method2(); } ... } </code></pre> <p>Of course, I can do this (aggregation of base implementation):</p> <pre><code> public RepeaterOfBase: Base // no any overrides, just inheritance { } public class Proxy: Base { private B _b; private RepeaterOfBase _Base; public Proxy(B b, RepeaterOfBase aBase) { _b = b; _base = aBase; } } ... public Base GetStateDependentImplementation() { if (State == Running) return _b; else return _Base; } ... </code></pre> <p>But instance of Base class is very huge and I have to avoid to have other additional copy in memory.</p> <p>So I </p> <ul> <li>have to simplify my code</li> <li>have to "wrap" implementation</li> <li>have to avoid a <em>code</em> duplication</li> <li>have to avoid aggregation of any <em>additional instance of Base class</em> (duplication)</li> </ul> <p>Is it possible to reach these goals?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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