Alex Rivera | Logout

How extension methods are implemented internally

Asked 2011-07-01T05:13:17.303
9

How are extension methods implemented internally? I mean what happens when the compiler sees a declaration for an extension method and what happens at runtime when there is a call to an extension method.

Is reflection involved? Or when you have an extension method is its code injected in the target class type metadata with some additional flags noting that this is an extension method and then the CLR knows how to handle that?

So in general, what happens under the hood?

Edit
Report

2 Answers

2

I dont think that reflection is involved in extension methods. The extension method is handled in the same way like you write a static helper function in a helper class, the only difference is that compiler does it for you.

answered 2011-07-01T05:15:56.680
1

To clarify the above answer... Extension Methods ARE static functions. The additional features in .NET 3.5 allow them to be interpreted as if they are new methods on the type in question.

answered 2011-07-01T05:17:32.027

Your Answer