0

I'm overriding some existing codes but try not to touch the existing classes.

I have a base class

public class A {
    public virtual void Func() {
        do something...
    }
}

I have several other classes is accessing it by calling A.Func()

I'm writing an extension class

public class B : A {
    public override void Func() {
        do something else...
    }
}

Without touching the base and those classes calling the base class, is there any way to point the method Func() to the extended class?

I mean when there's a class calling A.Func(), it executes the B.Func() instead A.Func() But without touching the code from that class nor the A class

An example

public class C {
    void SomeOutsideFunc()
    {
        var a = new A();
        a.Func(); //in here, is it possible to execute the extended B class's Func()?
    }
}

in above example class A and class C is forbidden for any modification class B is an extended class I coded. right now, I have to make another override to class C to make things working my issue is there are a lot of C type classes (none of those that I can make any changes)

tks

Joe Lu
  • 2,240
  • 1
  • 15
  • 18
  • 1
    no possible at all. so let's suppose there are `C: A`, `D: A`, `E:A` which method do you want to call? – Vivek Nuna Jul 21 '20 at 04:12
  • 2
    I have a feeling that you haven't tested this, because for an instance of `B` stored in variable of type `A`, calling `A.Func()` calls the derived method. – ProgrammingLlama Jul 21 '20 at 04:13
  • "`when there's a class calling A.Func(), it executes the B.Func() instead A.Func()` - could you include an example of that? – devNull Jul 21 '20 at 04:20
  • tks for all the quick replies, I kinda figured it is quite not possible but I edit my question with the example anyway, let me know if there's any workaround. The issue is that at this stage, I'm not allowed to touch the A class and that example C class :( – Joe Lu Jul 21 '20 at 04:51
  • Following your edit, I'll say that (as @viveknuna says) it's not really possible to replace `A` in that method. I don't think it's impossible as I've done things as crazy as replacing the get/set methods of an object at runtime in the past (for academic purposes, not for production code), but I don't know if that would be possible here, and I certainly wouldn't recommend it for any production code even if it were. – ProgrammingLlama Jul 21 '20 at 05:01
  • Hi John, I was told it is not possible, so I was kinda seeking workarounds from the mighty stackoverflow, lol. But I have to say your answer is very useful and points me to rethink some crazy stuff.. nevertheless, not as crazy as replacing get/set... I mean really? haha – Joe Lu Jul 21 '20 at 05:04
  • @Joe It was for "testing" the licensing mechanism of a library I was experimenting with. I think it might have been [this method](https://stackoverflow.com/a/55026523/3181933) that I used. :) – ProgrammingLlama Jul 21 '20 at 05:52

2 Answers2

1

You can create the object of base class which is pointing to the child class, suppose B.

A objA=new B(); objA.Func();

This will call the function of derived class.

1

What you're describing is how things already work. Imagine you have these two classes:

public class BaseClass
{
    public virtual void WriteSomething()
    {
        Console.WriteLine("base write something");
    }
}

public class DerivedClass : BaseClass
{
    public override void WriteSomething()
    {
        Console.WriteLine("derived write something");
    }
}

If you create an instance of BaseClass and call WriteSomething, you get "base write something":

BaseClass instance = new BaseClass();
instance.WriteSomething();
// "base write something" is printed to the console

Likewise, creating an instance of DerivedClass and calling WriteSomething results in "derived write something":

DerivedClass instance = new DerivedClass();
instance.WriteSomething();
// "derived write something" is printed to the console

Even if we assign that instance to a BaseClass variable, we still get "derived write something":

DerivedClass instance = new DerivedClass();
BaseClass instanceAsBaseClass = instance;
instanceAsBaseClass.WriteSomething();
// "derived write something" is printed to the console

So we can clearly then pass this into a method as BaseClass but still use the overriden methods from the derived class:

public static void DoSomeThings(BaseClass instance)
{
    instance.WriteSomething();
}

BaseClass instance = new DerivedClass();
DoSomeThings(instance);
// "derived write something" is printed to the console

Note that we haven't changed the DoSomeThings method.

Try it online

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • Hi John, tks for the quick reply, your answer is getting close to solve my question. However, from your code, the method is not changed, but it still requires me to add the BaseClass instance = new DerivedClass(), as my original post, I'm not allowed to make modifications to the calling classes – Joe Lu Jul 21 '20 at 04:58