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.
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 withExtensionAttribute. 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.