-2

I have a base class implementing an interface and further a specialized class inheriting the base class.

I have implemented interface's method in base class and marked it as virtual, also overridden the same method in specialized class.

Now i want to resolve the method GetData() on some basis that it either returns base class's method or child class's method.

So basically how can I call base class method using the specialized class's reference or interface's reference?

Edit 1 I have an existing data provider and I want to keep its functionality as it is and want to use some subclass or wrapper class where i can write a new implementation(another provider), mind that I want to keep running existing provider as it is for existing scenario and the new provider for other scenarios). what if i use decorator pattern to solve this? Any other pattern which can solve this ?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    interface IDataProvider
    {
        void GetData();
    }

     abstract class StandardDataProvider : IDataProvider
    {
        public virtual void GetData()
        {
            Console.WriteLine("GetData_StandardDataProvider");
        }
    }

    class ManagedDataProvider : StandardDataProvider
    {

        public override void GetData()
        {
            Console.WriteLine("GetData_ManagedDataProvider");
        }

    }


    class Program
    {
        static void Main(string[] args)
        {       

            IDataProvider dataprovider = new  ManagedDataProvider();

            dataprovider.GetData();

            Console.ReadLine();
        }
    }
}
  • Virtual/overriden methods are *specifically* designed to prevent this behavior. If you want to be able to access `StandardDataProvider` members that are overridden by `ManagedDataProvider`, then `ManagedDataProvider` will have to expose a new method to get at them. – BJ Myers Oct 11 '18 at 22:35
  • 4
    In my opinion, whatever you try to solve with this approach, this approach looks flawed to me. You have a base class that defines some action/behavior "GetData()". Then the concrete class inheriting from this base class is overriding this action/behavior. Now you want to choose whether the concrete class will do the overridden behavior or not on some arbitrary external circumstances. This is not well thought out... –  Oct 11 '18 at 22:36
  • I don't understand why you would override something and then not use it? If you want the base functionality, do not override it in that type. – CodingYoshi Oct 11 '18 at 22:37
  • I am not sure if that would work for you, but you could perhaps bake the decision of which action to do into the _GetData()_ override in _ManagedDataProvider_, like: `public override void GetData() { if (shouldUseBaseImplementation) base.GetData(); else Console.WriteLine("GetData_ManagedDataProvider"); }` –  Oct 11 '18 at 22:44
  • Possible duplicate of [Is there any way to call the parent version of an overridden method? (C# .NET)](https://stackoverflow.com/questions/438939/is-there-any-way-to-call-the-parent-version-of-an-overridden-method-c-net) – pere57 Oct 12 '18 at 00:58
  • @elgonzo ,I have an existing data provider and I want to keep its functionality as it is and want to use some subclass or wrapper class where i can write a new implementation(another provider), mind that I want to keep running existing provider as it is for existing scenario and the new provider for other scenarios). what if i use decorator pattern to solve this? Any other pattern which can solve this ? – Pankaj Bhatia Oct 12 '18 at 10:06

1 Answers1

0

This is the only solution I could come up with for your problem:

interface IDataProvider
{
    void GetData();
}

abstract class StandardDataProvider : IDataProvider
{
    public virtual void GetData()
    {
        Console.WriteLine("GetData_StandardDataProvider");
    }
}

class ManagedDataProvider : StandardDataProvider
{

    public override void GetData()
    {
        Console.WriteLine("GetData_ManagedDataProvider");
    }

    public void GetBaseData()
    {
        base.GetData();
    }
}



class Program
{
    static void Main(string[] args)
    {

        IDataProvider dataprovider = new ManagedDataProvider();

        dataprovider.GetData();
        if (dataprovider is ManagedDataProvider)
            {
                (dataprovider as ManagedDataProvider).GetBaseData();
            }


        Console.ReadLine();
    }
}

Another Way to attack it is adding GetBaseData to the Interface.

interface IDataProvider
{
    void GetData();
    void GetBaseData();
}

abstract class StandardDataProvider : IDataProvider
{
    public virtual void GetData()
    {
        Console.WriteLine("GetData_StandardDataProvider");
    }

    public virtual void GetBaseData()
    {
        Console.WriteLine("GetData_StandardDataProvider");
    }
}

class ManagedDataProvider : StandardDataProvider
{

    public override void GetData()
    {
        Console.WriteLine("GetData_ManagedDataProvider");
    }

    public override void GetBaseData()
    {
        base.GetData();
    }
}



class Program
{
    static void Main(string[] args)
    {

        IDataProvider dataprovider = new ManagedDataProvider();

        dataprovider.GetData();
        dataprovider.GetBaseData();            

        Console.ReadLine();
    }
}
Kelly Barnard
  • 1,131
  • 6
  • 8