Is there any way to overload extension methods in

2019-03-21 02:56发布

问题:

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).

回答1:

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) {
    ...
  }
}


回答2:

Unfortunately I don't think you will be able to get what you want here. Extension methods are static, and static methods cannot be virtual.

You could work around this with something like JaredPar's solution.

If your goal is to separate your model from some implementation, I suggest you look into the Bridge Pattern (GOF). "Separate an abstraction from an implementation" This may help to separate your concerns and keep your model class cleaner.