0

I have a dll file, which include various derived class which inherits from base class and this base class have a virtual method named "Run" and in derived classes there is no overridden "Run" method. Now, the behaivior of method Run change in specific case and I want to apply for all derived class so if I want change to that behaivior, normally I have to create various custom derived class class which inherit from derived class and override "Run" method. It is burdensome if the number of derived class is many and it cause the duplicate source. Note I dont have permit to modify any existing in that dll file.

dll file:

public abstract class Animal
{
    public virtual void Run()
    {
        Console.WriteLine("Hello 1");
    }
}

public class Dogs : Animal
{
    //do other thing.
}

public class Cat : Animal
{
    //do other thing.
}

client side nomally:

public class CusTomDogs : Dogs
{
    public override void Run()
    {
        Console.WriteLine("Hello 2");
    }
}

public class CusTomCat : Cat
{
    public override void Run()
    {
        Console.WriteLine("Hello 2");
    }
}

Is there possible way to change behavior "base.Run()" method without create custom each derrived class?

Phong Dao
  • 139
  • 1
  • 1
  • 14

2 Answers2

0

The thing you are trying to do is "Multiple inheritance", that is not permitted in .Net.

You have to make changes in base class or change each inherited class.

Cid
  • 14,968
  • 4
  • 30
  • 45
Timur Lemeshko
  • 2,747
  • 5
  • 27
  • 39
  • I'm curious, how do you see multiple inheritance helping here? – Rotem Feb 18 '19 at 13:40
  • what we need here is to inject onother behavior between Dogs and Animal: change Run() without changing Dogs and Animal, so we need to inherit CusTomDogs from Dogs and from another class, which override Animal.Run(). We can do it with PROXY pattern, but Phong Dao say than whe have lot of CusTomDogs, CusTomCat and so on – Timur Lemeshko Feb 18 '19 at 13:48
  • But you still need to inherit from all the subclasses that you want to change, so you're not much better off than the solution in the OP. I guess you did save a line in each subclass having to override the `Run` method. – Rotem Feb 18 '19 at 13:51
-2

You could have a single "Custom" class where you can inject the behaviour you want for the Run method.

public class CustomAnimal : Animal
{
    public Action RunAction { get; set; }

    public override void Run()
    {
        RunAction();
    }
}

Then you can just assign what you want it to invoke at runtime

Martin Grundy
  • 254
  • 2
  • 11
  • How does this affect the behavior of a different class which inherits from `Animal`? – Rotem Feb 18 '19 at 12:36
  • I thought he wanted to change the functionality of the Run() method without creating multiple derrived classes. Maybe I misunderstood his problem. – Martin Grundy Feb 18 '19 at 12:41
  • There are already classes deriving from `Animal` which he does not control but must use. – Rotem Feb 18 '19 at 12:42
  • @Rotem but this solution could be adapted, I think. So the dll provides `Dogs` and `Cat` as "API", so OP could derive from them using above pattern. Suboptimal due to duplicate code. But until I or someone else cannot come up with something better, I guess it will be as good as it gets. Just I would use CTOR injection for the "RunBehavior". – Fildor Feb 18 '19 at 12:54
  • @Fildor That's the solution already described in the OP. The whole question is about avoiding this solution. – Rotem Feb 18 '19 at 12:56
  • @Rotem Not exactly. Injecting the RunBehavior would minimize the dupe code. It doesn't fit exactly the OP's requirement to not having to subclass at all, but I don't see an obvious way around it. – Fildor Feb 18 '19 at 12:58