Keep in mind that extension methods are compiler tricks. If you look at the static method in the static class where the extension method is defined, you can call it just fine.
Now, if all you have is an object, and you are trying to find a specific extension method, you can find the appropriate extension method by searching for all your static classes in the application domain for methods that have System.Runtime.CompilerServices.ExtensionAttribute and The specific method name and the corresponding sequence of parameters.
This approach will fail if two extension classes define an extension method with the same name and signature. It will also fail if the assembly is not uploaded to the application domain.
A simple approach is this (if you are looking for a general method):
static class Extensions { public static T Echo<T>(this T obj) { return obj; } } class Program { static void Main(string[] args) { Console.WriteLine("hello".Echo()); var mi = typeof(Extensions).GetMethod("Echo"); var generic = mi.MakeGenericMethod(typeof(string)); Console.WriteLine(generic.Invoke(null, new object[] { "hello" })); Console.ReadKey(); } }
source share