Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reflection to Identify Extension Methods

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.

like image 458
Mike Chess Avatar asked Nov 18 '08 17:11

Mike Chess


People also ask

What is the main purpose of an extension method?

Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type.

How do you find the extension method?

An extension method must be defined in a top-level static class. An extension method with the same name and signature as an instance method will not be called. Extension methods cannot be used to override existing methods. The concept of extension methods cannot be applied to fields, properties or events.

Can we define extension method for a class?

An extension method is actually a special kind of static method defined in a static class. To define an extension method, first of all, define a static class. For example, we have created an IntExtensions class under the ExtensionMethods namespace in the following example.

Should you use extension methods?

Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.


1 Answers

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 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 method.GetParameters()[0].ParameterType == extendedType                     select method;         return query;     } } 
like image 80
Jon Skeet Avatar answered Sep 26 '22 21:09

Jon Skeet