Now we can call its methods as usual:public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
public class TestClass { public void TestMethod1() { Console .WriteLine( "test method 1" ); } public void TestMethod2() { Console .WriteLine( "test method 2" ); } } * This source code was highlighted with Source Code Highlighter .
As we expected, everything compiled perfectly. Now use the new dynamic keyword :* This source code was highlighted with Source Code Highlighter .
- var test = new TestClass ();
- test.TestMethod1 ();
- test.TestMethod2 ();
Nothing changed? By no means. Everything is compiled the same way as before, but now the method calls will be bound at run time, not compilation. Ie, if we write this:* This source code was highlighted with Source Code Highlighter .
- dynamic test = new TestClass ();
- test.TestMethod1 ();
- test.TestMethod2 ();
The code will compile anyway, but after launch we will get this exception:* This source code was highlighted with Source Code Highlighter .
- dynamic test = new TestClass ();
- test.TestMethod1 ();
- test.TestMethod2 ();
- test.TestMethod3 ();
First, the compiler generated the o__SiteContainer0 field to store information about the place of the call. If you look at the test variable that contains our object, you will see that its type has become the type object . So, in reality, this is not a dynamic type, but only an assistant from the compiler. Next comes the check of call sites ( callsite) to null and if they are equal to it, then CallSite.Create is used, to which the CSharpCallPayload object is sent , which contains all the information about the method we call. As soon as the place of the call is specified, it is simply called on our test variable. As you can see, the compiler did everything for us.* This source code was highlighted with Source Code Highlighter .
- private static void Main ( string [] args)
- {
- object test = new TestClass ();
- if (<Main> o__SiteContainer0. <> p__Site1 == null )
- {
- <Main> o__SiteContainer0. <> P__Site1 = CallSite <Action <CallSite, object >>. Create ( new CSharpCallPayload (Microsoft.CSharp.RuntimeBinder.RuntimeBinder.GetInstance (), false , false , "TestMethod1" , typeof ( object ), null ))
- }
- <Main> o__SiteContainer0. <> P__Site1.Target (<Main> o__SiteContainer0. <> P__Site1, test);
- if (<Main> o__SiteContainer0. <> p__Site2 == null )
- {
- <Main> o__SiteContainer0. <> P__Site2 = CallSite <Action <CallSite, object >>. Create ( new CSharpCallPayload (Microsoft.CSharp.RuntimeBinder.RuntimeBinder.GetInstance (), false , false , "TestMethod2" , typeof ( object ), null ))
- }
- <Main> o__SiteContainer0. <> P__Site2.Target (<Main> o__SiteContainer0. <> P__Site2, test);
- }
Source: https://habr.com/ru/post/43773/
All Articles