Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to overload extension methods in C#?

I have the following Model pattern:

public abstract class PARENTCLASS {...}
public class CHILD_A_CLASS : PARENTCLASS{...}
public static class EXTENSION{
  public static METHOD(this PARENTCLASS parent){...}
  public static METHOD(this CHILD_A_CLASS child) {...}
}

Something like above, of course there will be more child (and grandchild) classes but I just put one of them. The problem is, when I called the extension method like the following:

PARENTCLASS cc = new CHILD_A_CLASS();
cc.METHOD();

It will execute the PARENT Extension Method instead of my-expected CHILD extension method. Anyone have idea on how to implement this? (I'm not considering putting the METHOD itself into the class and let it do inheritance because I want to keep the model class clean and away from other logic).

like image 322
xandy Avatar asked May 24 '09 02:05

xandy


1 Answers

It is certainly possible to overload extension methods. Your code is an example of exactly how to do so.

What you appear to want though is the ability to override extension methods in such a way that the runtime type of the object will determine the extension method call. Much like defining a virtual method on a class. There is no specific language syntax support for such a feature.

If this is really important to you though, it's possible to hand implement the feature. It requires a bit of brute force but it will get the job done. For example ...

public static class Extension {
  public static void Method(this ParentClass p) { 
    var c = p as ChildAClass;
    if ( c != null ) {
      Method(c);
    } else {
      // Do parentclass action
    }
  }
  public static void Method(this ChildAClass c) {
    ...
  }
}
like image 88
JaredPar Avatar answered Sep 27 '22 21:09

JaredPar