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.