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

  • Home
  • SEARCH
  • 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 82855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:40:56+00:00 2026-05-10T21:40:56+00:00

In C# is there a technique using reflection to determine if a method has

  • 0

In C# is there a technique using reflection to determine if a method has been added to a class as an extension method?

Given an extension method such as the one shown below is it possible to determine that Reverse() has been added to the string class?

public static class StringExtensions {     public static string Reverse(this string value)     {         char[] cArray = value.ToCharArray();         Array.Reverse(cArray);         return new string(cArray);     } } 

We’re looking for a mechanism to determine in unit testing that the extension method was appropriately added by the developer. One reason to attempt this is that it is possible that a similar method would be added to the actual class by the developer and, if it was, the compiler will pick that method up.

  • 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. 2026-05-10T21:40:57+00:00Added an answer on May 10, 2026 at 9:40 pm

    You have to look in all the assemblies where the extension method may be defined.

    Look for classes decorated with ExtensionAttribute, and then methods within that class which are also decorated with ExtensionAttribute. Then check the type of the first parameter to see if it matches the type you’re interested in.

    Here’s some complete code. It could be more rigorous (it’s not checking that the type isn’t nested, or that there is at least one parameter) but it should give you a helping hand.

    using System; using System.Runtime.CompilerServices; using System.Reflection; using System.Linq; using System.Collections.Generic;  public static class FirstExtensions {     public static void Foo(this string x) { }     public static void Bar(string x) { } // Not an ext. method     public static void Baz(this int x) { } // Not on string }  public static class SecondExtensions {     public static void Quux(this string x) { } }  public class Test {     static void Main()     {         Assembly thisAssembly = typeof(Test).Assembly;         foreach (MethodInfo method in GetExtensionMethods(thisAssembly, typeof(string)))         {             Console.WriteLine(method);         }     }     static IEnumerable<MethodInfo> GetExtensionMethods(Assembly assembly, Type extendedType)     {         var isGenericTypeDefinition = extendedType.IsGenericType && extendedType.IsTypeDefinition;         var query = from type in assembly.GetTypes()             where type.IsSealed && !type.IsGenericType && !type.IsNested             from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)             where method.IsDefined(typeof(ExtensionAttribute), false)             where isGenericTypeDefinition                 ? method.GetParameters()[0].ParameterType.IsGenericType && method.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == extendedType                 : method.GetParameters()[0].ParameterType == extendedType             select method;         return query;     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer While SQL Server has nothing to offer by way of… May 11, 2026 at 2:07 pm
  • added an answer Are the form elements that are being populated from your… May 11, 2026 at 2:07 pm
  • added an answer if you are going to use jQuery, you can easily… May 11, 2026 at 2:07 pm

Related Questions

In C# Is there a way of getting a finer granularity with SQL exceptions?
In C# is there a way to detect if a string is all caps?
Given a List<T> in c# is there a way to extend it (within its
In C# there is a method SetApartmentState in the class Thread . How do
I need to knock out a quick animation in C#/Windows Forms for a Halloween
How do you properly override isEqual: in Objective-C? The catch seems to be that
In my PHP application ( constructed using symfony framework and Propel ORM), when I
We are in evaluating technologies that we'll use to store data that we gather

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.