c# - How extension methods are implemented internally -
how extension methods implemented internally? mean happens when compiler sees declaration extension method , happens @ runtime when there call extension method.
is reflection involved? or when have extension method code injected in target class type metadata additional flags noting extension method , clr knows how handle that?
so in general, happens under hood?
as have said other colleagues static method. compiler can clr have no idea extension methods. can try check il code ..
here example
static class extendedstring { public static string testmethod(this string str, string someparam) { return someparam; } } static void main(string[] args) { string str = string.empty; console.writeline(str.testmethod("hello world!!")); ........ }
and here il code.
il_0001: ldsfld string [mscorlib]system.string::empty il_0006: stloc.0 il_0007: ldloc.0 il_0008: ldstr "hello world!!" il_000d: call string stringpooling.extendedstring::testmethod(string, string) il_0012: call void [mscorlib]system.console::writeline(string) il_0017: nop
as can see call of static method. method not added class, compiler makes that. , on reflection layer difference can see compilerservices.extensionattribute added.
Comments
Post a Comment