Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you don't want to introduce dependencies, like inheritance or interfaces to implement on MyClass then:</p> <p>You can achieve this through delegates.</p> <p>Long story short, you cannot override function in runtime without resorting to some obscure reflection magic, but you can declare delegates instead of functions. When you construct your class in the constructor fill the delegates with private methods which will be used for as long as no AttachmentA class comes in. And use those delegates instead of the methods.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.Runtime.InteropServices; namespace ConsoleApplication1 { static class Program { static void Main(string[] args) { MyClass aaa = new MyClass(); aaa.A(); // should print MyClass.A aaa.B(); // should print MyClass.B aaa.Attach(new AttachmentA()); aaa.Attach(new AttachmentB()); aaa.A(); // should print AttachmentA.A &lt;newline&gt; MyClass.A aaa.B(); // should print AttachmentB.B } } class MyClass { public List&lt;Attachment&gt; Attachments; public MyClass() { A = _A; B = _B; Attachments = new List&lt;Attachment&gt;(); } public void Attach(Attachment attachment) { Attachments.Add(attachment); // this is your magic if (attachment.GetType() == typeof(AttachmentA)) { A = ((AttachmentA)attachment).A; } else if (attachment.GetType() == typeof(AttachmentB)) { B = ((AttachmentB)attachment).B; } } public delegate void delegateA(); public delegate void delegateB(); public delegateA A; public delegateB B; public void _A() { Console.WriteLine("MyClass.A"); } public void _B() { Console.WriteLine("MyClass.B"); } } class Attachment { } class AttachmentA : Attachment { public void A() { Console.WriteLine("AttachmentA.A"); } } class AttachmentB : Attachment { public void B() { Console.WriteLine("AttachmentB.B"); } } } </code></pre> <p>If you need the execution to start always in MyClass instead of the Attachment class you can wrap the delegates like here:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.Runtime.InteropServices; namespace ConsoleApplication1 { static class Program { static void Main(string[] args) { MyClass aaa = new MyClass(); aaa.A(); // should print MyClass.A aaa.B(); // should print MyClass.B aaa.Attach(new AttachmentA()); aaa.Attach(new AttachmentB()); aaa.A(); // should print AttachmentA.A &lt;newline&gt; MyClass.A aaa.B(); // should print AttachmentB.B } } class MyClass { public List&lt;Attachment&gt; Attachments; public MyClass() { Attachments = new List&lt;Attachment&gt;(); } public void Attach(Attachment attachment) { Attachments.Add(attachment); if (attachment.GetType() == typeof(AttachmentA)) { _A = ((AttachmentA)attachment).A; } else if (attachment.GetType() == typeof(AttachmentB)) { _B = ((AttachmentB)attachment).B; } } public delegate void delegateA(); public delegate void delegateB(); public delegateA _A; public delegateB _B; public void A() { if (_A != null) { _A(); } else { Console.WriteLine("MyClass.A"); } } public void B() { if (_B != null) { _B(); } else { Console.WriteLine("MyClass.B"); } } } class Attachment { } class AttachmentA : Attachment { public void A() { Console.WriteLine("AttachmentA.A"); } } class AttachmentB : Attachment { public void B() { Console.WriteLine("AttachmentB.B"); } } } </code></pre> <p>You can shorten this to one delegate type if A and B have the same parameters and return type in your real scenario.</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