1

Just a disclaimer I am very new to extension methods and do not really know the "Dos and Don'ts" associated with them and I suspect what I'm trying to do is a don't so if it is simply suggest a potential restructure to my code that would work.

Alright, so the best way to explain what I want to do is through a code example.

namespace ReferencedLibrary
{
     public class Bar
    {
        public Bar(IFoo foo)
        {
            foo.GetLocation();
        }
    }
    public interface IFoo { }
    public static ExtensionMethods
    {
        public static void GetLocation(this IFoo foo){throw new Exception("ERROR: This should not be called.....");}
    }

}
namespace PrimaryApplication
{
    public class MainController
    {
        Bar bar = new Bar(aClassThatImplementsIFoo);
    }
    public static class OtherExtensionMethods
    {
        public static void GetLocation(this IFoo foo)
        {
            Console.WriteLine("Hello World");
        }
        public static void GetWheelConfiguration(this IFoo foo)
        {
            Console.WriteLine("Hello World");
        }
    }
    //This is an example of the inheritance structure of the data I'm using and is unchangeable no matter what.
    public class Vehicle {}
    public class Car : Vehicle {}
    public class Toyota : Car {}
    public class Holden : Car, IFoo {}

    public class Bike : Vehicle IFoo {}
}

I am attempting to use somebody else's code that I would like to be able to keep in a separate project and reference the dll. I also would like to avoid modifying this code too much. That being said if need be I can simply bring it into the PrimaryApplication if need be.

There are a few solutions such as using the interface as it probably should be used and create a method getLocation() in each of the classes that implement IFoo however there is about 4-5 of them and this seems unclean to me.

Anyway, what would you propose as being the best/cleanest/easiest-to-maintain solution to what I am attempting to achieve?

Thanks

EDIT: This question is similar to : How to override an existing extension method However the given answer is not sufficient for me as I call the extension method from within the referenced library, not the primary application therefore I cannot go

using PrimaryApplication
{

}

Edit: This question has been answered by icemaninid in the comments and is clearly not a duplicate considering the answer is not the same as the answer to the other thread.

Community
  • 1
  • 1
Adrian773
  • 473
  • 11
  • 22
  • 1
    Since they are static, you can not make them virtual. However, look at the accepted answer here for a "workaround": http://stackoverflow.com/questions/1451099/how-to-override-an-existing-extension-method – Icemanind Jan 16 '15 at 04:20
  • @icemanind how would you propose I implement that workaround? The extension method is called within the referenced library which does not reference the primary application. – Adrian773 Jan 18 '15 at 20:07
  • Perhaps extension methods aren't the way to go. Perhaps what you should do is subclass. Try creating a new class in your program that inherits this other class. – Icemanind Jan 18 '15 at 20:31
  • @icemanind Do you mean have my data structure inherit from another class? So make it inherit from a "Locatable" class and "Vehicle" class? – Adrian773 Jan 18 '15 at 22:42
  • I'm not sure exactly what you are trying to do. But say you have a DLL assembly that contains a Vehicle class and you don't want to modify it, but you need it to behave different. Create a new class called Vehicle2 that inherits Vehicle. Then you can add your own functionality. – Icemanind Jan 19 '15 at 00:21
  • Well what I am actually trying to do this for is a quadtree and I need to allow the quadtree library (not my own, however it was not working correctly so I have created my own version from the original) to extract a Rect object out of my own custom objects (Bike, Vehicle etc), however the code to extract that Rect is complex and I do not wish to move it to this quadtree library. Does that put what I'm attempting to do in a little more context? – Adrian773 Jan 19 '15 at 01:02
  • 1
    Yes. I would create a new class, inheriting the quadtree class. Then create your own method called ExtractRect or something. – Icemanind Jan 19 '15 at 05:27
  • Ahh that works, thanks. Would you like to put it as an answer and I will accept it? – Adrian773 Jan 19 '15 at 20:06
  • The question was closed, so I can't post a new answer. – Icemanind Jan 20 '15 at 01:43
  • Any idea how to get it opened? – Adrian773 Jan 20 '15 at 20:05

1 Answers1

2

You cannot override an extension method. They are static methods resolved at compile time, not at runtime.

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182