Note that there are some explanatory texts on larger screens.

plurals
  1. POInsertion of classes in the inheritance tree
    text
    copied!<p>I wonder if it would be possible to insert classes in an inheritance tree at arbitrary positions.</p> <p>Example:</p> <pre><code>class Base { public virtual void DoSomething() { Console.WriteLine("Base"); } } class D1:Base { public override void DoSomething() { base.DoSomething(); Console.WriteLine("D1"); } } class D2:Base { public override void DoSomething() { base.DoSomething(); Console.WriteLine("D2"); } } class D3:Base { public override void DoSomething() { base.DoSomething(); Console.WriteLine("D3"); } } </code></pre> <p>Imagine the above classes are from a library with no source available. If I want to modify the behaviour of all the derived classes I have to inherit from all of them and implement the same thing multiple times. Then I have to change all types to my newly derived one. An easier way would be to insert a class directly after Base and somehow (that is probably a big somehow) make the derived class point to that as their base object. The derived classes could then be used unchanged.</p> <pre><code>class Inserted : after Base { public override void DoSomething() { base.DoSomething(); Console.WriteLine("Inserted"); } } </code></pre> <p>or</p> <pre><code>class Inserted : before D1 { public override void DoSomething() { base.DoSomething(); Console.WriteLine("Inserted"); } } </code></pre> <p>The keywords "before" and "after" would control where exactly the class is to be inserted.</p> <p>In the past I worked with a system that would support something like this and it was a huge productivity boost.</p> <p>Is this principially possible with for instance, the .Net CLR or with the Java Runtime? Would it be possible to change the v_table to point to the new base class? Why does no modern system support this? What would the ramifications be regarding reliability, security and so on?</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