Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 876283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:24:33+00:00 2026-05-15T11:24:33+00:00

I had a discussion in another thread, and found out that class methods takes

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T11:24:34+00:00Added an answer on May 15, 2026 at 11:24 am

    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.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.CompilerServices;
    
    public class ExtensionCollisionDetector
    {
        private static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine
                    ("Usage: ExtensionCollisionDetector <assembly file> [...]");
                return;
            }
            foreach (string file in args)
            {
                Console.WriteLine("Testing {0}...", file);
                DetectCollisions(file);
            }
        }
    
        private static void DetectCollisions(string file)
        {
            try
            {
                Assembly assembly = Assembly.LoadFrom(file);
                foreach (var method in FindExtensionMethods(assembly))
                {
                    DetectCollisions(method);
                }
            }
            catch (Exception e)
            {
                // Yes, I know catching exception is generally bad. But hey,
                // "something's" gone wrong. It's not going to do any harm to
                // just go onto the next file.
                Console.WriteLine("Error detecting collisions: {0}", e.Message);
            }
        }
    
        private static IEnumerable<MethodBase> FindExtensionMethods
            (Assembly assembly)
        {
            return from type in assembly.GetTypes()
                   from method in type.GetMethods(BindingFlags.Static |
                                                  BindingFlags.Public |
                                                  BindingFlags.NonPublic)
                   where method.IsDefined(typeof(ExtensionAttribute), false)
                   select method;
        }
    
    
        private static void DetectCollisions(MethodBase method)
        {
            Console.WriteLine("  Testing {0}.{1}", 
                              method.DeclaringType.Name, method.Name);
            Type extendedType = method.GetParameters()[0].ParameterType;
            foreach (var type in GetTypeAndAncestors(extendedType).Distinct())
            {
                foreach (var collision in DetectCollidingMethods(method, type))
                {
                    Console.WriteLine("    Possible collision in {0}: {1}",
                                      collision.DeclaringType.Name, collision);
                }
            }
        }
    
        private static IEnumerable<Type> GetTypeAndAncestors(Type type)
        {
            yield return type;
            if (type.BaseType != null)
            {
                // I want yield foreach!
                foreach (var t in GetTypeAndAncestors(type.BaseType))
                {
                    yield return t;
                }
            }
            foreach (var t in type.GetInterfaces()
                                  .SelectMany(iface => GetTypeAndAncestors(iface)))
            {
                yield return t;
            }        
        }
    
        private static IEnumerable<MethodBase>
            DetectCollidingMethods(MethodBase extensionMethod, Type type)
        {
            // Very, very crude to start with
            return type.GetMethods(BindingFlags.Instance |
                                   BindingFlags.Public |
                                   BindingFlags.NonPublic)
                       .Where(candidate => candidate.Name == extensionMethod.Name);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.