Polymorphism Through Extension Methods?

Viewed 3307

I have a class library which contain some base classes and others that are derived from them. In this class library, I'm taking advantage of polymorphism to do what I want it to do. Now in a consuming application, I want to change the behavior of some code based on the runtime type of the child classes. So assume the following:

public class Base { }
public class Child1 : Base { }
public class Child2 : Base { }

Now in the consuming application I want do something as follows (note that all of the following classes are in the consuming application and cannot be referenced in the class library):

public interface IMyInterface1 { }
public interface IMyInterface2 { }
public static class Extensions
{
    public static void DoSomething(this Base myObj, Object dependency)
    {

    }

    public static void DoSomething(this Child1 myObj, Object dependency)
    {
        IMyInterface1 myInterface = dependency as IMyInterface1;
        if (myInterface != null)
        {
            //Do some Child1 specific logic here
        }
    }

    public static void DoSomething(this Child2 myObj, Object dependency)
    {
        IMyInterface2 myInterface = dependency as IMyInterface2;
        if (myInterface != null)
        {
            //Do some Child2 specific logic here
        }
    }
}

UPDATE:

This does not work. It always calls the extension method of the base class. Is there some other way that will allow me to do this and avoid having to explicitly check for the runtime type? The reasons is because more classes that are derived from the Base could be added and corresponding extension methods could come from some other external assembly.

Thanks in advance.

5 Answers

Below is the minimal example showing how to mimic polymorphism with extension methods.

void Main()
{
    var elements = new Base[]{
        new Base(){  Name = "Base instance"},
        new D1(){    Name = "D1 instance"},
        new D2(){    Name = "D2 instance"},
        new D3(){    Name = "D3 instance"}

    };

    foreach(Base x in elements){
        x.Process();
    }
}

public class Base{
    public string Name;
}
public class D1 : Base {}
public class D2 : Base {}
public class D3 : Base {}


public static class Exts{

    public static void Process(this Base obj){
        if(obj.GetType() == typeof(Base)) Process<Base>(obj); //prevent infinite recursion for Base instances
        else Process((dynamic) obj);
    }

    private static void Process<T>(this T obj) where T: Base
    {
        Console.WriteLine("Base/Default: {0}", obj.Name);
    }

    public static void Process(this D1 obj){
        Console.WriteLine("D1: {0}", obj.Name);
    }

    public static void Process(this D2 obj){
        Console.WriteLine("D2: {0}", obj.Name);
    }
}

Outputs:

    Base/Default: Base instance
    D1: D1 instance
    D2: D2 instance
    Base/Default: D3 instance
Related