I had a discussion in another thread, and found out that class methods takes precedence over extension methods with the same name and parameters. This is good as extension methods won’t hijack methods, but assume you have added some extension methods to a third party library:
public class ThirdParty
{
}
public static class ThirdPartyExtensions
{
public static void MyMethod(this ThirdParty test)
{
Console.WriteLine("My extension method");
}
}
Works as expected: ThirdParty.MyMethod -> “My extension method”
But then ThirdParty updates it’s library and adds a method exactly like your extension method:
public class ThirdParty
{
public void MyMethod()
{
Console.WriteLine("Third party method");
}
}
public static class ThirdPartyExtensions
{
public static void MyMethod(this ThirdParty test)
{
Console.WriteLine("My extension method");
}
}
ThirdPart.MyMethod -> “Third party method”
Now suddenly code will behave different at runtime as the third party method has “hijacked” your extension method! The compiler doesn’t give any warnings.
Is there a way to enable such warnings or otherwise avoid this?
Nope – this is a known downside of extension methods, and something to be very careful about. Personally I wish that the C# compiler would warn you if you declared an extension method which would never be called other than via the normal static route (
ExtensionClassName.MethodName(target, ...)).It probably wouldn’t be too hard to write a little tool to examine all the extension methods in an assembly and issue warnings that way. It probably wouldn’t need to be very precise to start with: just warning if there’s already a method with the same name (without worrying about parameter types) would be a good start.
EDIT: Okay… here’s a very crude tool to at least give a starting point. It appears to work at least to some extent with generic types – but it’s not trying to do anything with parameter types or names… partly because that becomes tricky with parameter arrays. It also loads assemblies “fully” instead of with reflection only, which would be nicer – I tried the “proper” route, but ran into some problems which weren’t immediately trivial to resolve, so fell back to the quick and dirty route 🙂
Anyway, hopefully it’ll be useful to someone, somewhere.