Note that there are some explanatory texts on larger screens.

plurals
  1. POAttaching overrides to a C# class
    primarykey
    data
    text
    <p>I have a class with functions:</p> <pre><code>class MyClass { public List&lt;Attachment&gt; Attachments; public void A() { // Do something } public void B() { // Do something } } class AttachmentA : Attachment { public void A() { // Do something else RealA(); } } class AttachmentB : Attachment { public void B() { // Do something else // RealB(); // No need to call base function here } } </code></pre> <p>I need in my code when I attach <code>AttachmentA</code> to <code>MyClass</code> that all the functions in <code>MyClass</code> that are also present in <code>AttachmentA</code> to be overridden by the functions in <code>AttachmentA</code> and also give access to the original functions in <code>MyClass</code>.<br> For example, I create <code>MyClass</code> and then attach <code>AttachmentA</code> instance to it. calling <code>MyClass.A()</code> will actually call <code>AttachmentA.A()</code> and the <code>AttachmentA.RealA()</code> will call the base function that was overridden.<br> I know this can be somehow done with something like using event handlers lists to handle overrides but is there an easy way to implement this?<br> Edit: I have no problem with long code that uses reflection as long as its present once and doesn't have to be even mentioned in any of the functions - maybe only when attaching attachement.</p> <p>Edit: you wanted an example:</p> <pre><code>class MyClass { public List&lt;Attachment&gt; Attachments; public MyClass() { Attachments = new List&lt;Attachment&gt;(); } public void Attach(Attachment attachment) { Attachments.Add(attachment); // Do some magic here } public void A() { Console.WriteLine("MyClass.A"); } public void B() { Console.WriteLine("MyClass.B"); } } class AttachmentA : Attachment { public void A() { Console.WriteLine("AttachmentA.A"); RealA(); } } class AttachmentB : Attachment { public void B() { Console.WriteLine("AttachmentB.B"); } } public class Program { public 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 } } </code></pre> <p>Edit: What I want to achieve here is like unit with attributes( = attachments). When the unit get an attachment of <code>RandomSpeed</code>, <code>RandomSpeed</code> will override the unit's <code>GetSpeed</code> and return random value. when it will get an attachment of evasion, it will override that units <code>ReduceHP</code> function and sometimes based on random value will not call the base function.</p> <p>Edit: What will really solve this mess is to somehow use reflection to change virtual method tables, I'm gonna make a followup on a separate question. I keep this question here incase someone find a better way to do this.</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.
 

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