I am new to .Net and was going through extension methods. I wanted to know how can I invoke extension method by reflection? Will there be a performance impact? Any pointer for this will be useful.
Asked
Active
Viewed 176 times
3
-
Duplicate of http://stackoverflow.com/questions/1452261/how-do-i-invoke-an-extension-method-using-reflection – L-Four Jun 04 '13 at 13:08
1 Answers
1
Extension methods are just syntactic sugar to make your code look better. The only thing to remember is that they are actually static members of the static class, not methods on the type they are extending. Performance wise, invoking them through reflection is no different from invoking any other static method.
static class MyExtensions
{
public static void Foo(this int i)
{
// do something
}
}
var methodInfo = typeof(MyExtensions).GetMethod("Foo");
methodInfo.Invoke(null, new object[] { 1 });

p.s.w.g
- 146,324
- 30
- 291
- 331