C #4.0 one of the new features: Dynamic lookup (2)

Source: Internet
Author: User

Let's analyze what reflector sees:

Code
1 Private   Static   Void Main ( String [] ARGs)
2 {
3 Object  Foo1  =   New Foo ();
4 If ( < Main > O __ Sitecontainer0. <> P __ Site1  =   Null )
5 {
6 < Main > O _ sitecontainer0. <> P _ Site1 =   Callsite < Action < Callsite, Object > . Create( New Csharpcallpayload (Microsoft. CSHARP. runtimebinder. runtimebinder. getinstance (), False , False , " Do1 " , Typeof(Object ), Null ));
7 }
8 < Main > O _ sitecontainer0. <> P _ site1. Target( < Main > O _ sitecontainer0. <> P __ Site1, Foo1);
9 If ( < Main > O _ sitecontainer0. <> P _ Site2 =   Null )
10 {
11 < Main > O _ sitecontainer0. <> P _ Site2 = Callsite < Action < Callsite, Object > . Create ( New Csharpcallpayload (Microsoft. CSHARP. runtimebinder. runtimebinder. getinstance (), False , False , " Do2 " , Typeof ( Object ), Null ));
12 }
13 < Main > O _ sitecontainer0. <> P _ site2.target ( < Main > O _ sitecontainer0. <> P _ Site2, foo1 );
14 If ( < Main > O _ sitecontainer0. <> P _ site3 =   Null )
15 {
16 < Main > O _ sitecontainer0. <> P _ site3 = Callsite < Action < Callsite, Object > . Create ( New Csharpcallpayload (Microsoft. CSHARP. runtimebinder. runtimebinder. getinstance (), False , False , " Kissfanweixiao " , Typeof ( Object ), Null ));
17 }
18 < Main > O _ sitecontainer0. <> P _ site3.target ( < Main > O _ sitecontainer0. <> P _ site3, foo1 );
19 }

 

First, the compiler generates a local variable of _ sitecontainer0 to save the content of our callsite. Then we noticed that test is already of the object type. It seems that this is not a real "dynamic type", but a "Help method.

NextCodeIt checks whether the callsite is null. If yes, it uses callsite. Create and passes a csharpcallpayload object as the parameter. This object stores all information about the methods we will call. If this callsite is not null, it will "attach" all the objects in this method to the foo object.

The compiler implements these two steps to implement this dynamic function.

The above content comes from Justin etheredge's blog. The post-blog also compares dynamic efficiency issues. After his tests, the difference between VaR and dynamic makes the performance quite different. 6 MS: 2106 Ms. But when I first saw this test, I thought it was inappropriate. If you run the dynamicProgramWhen I started the program, I always thought that there was no reaction to the program. After a while, the result would come out, that is, the first handle of the runtime would have to be too time-consuming, however, this situation only occurs when loading data at the beginning. Therefore, this group of more efficient figures cannot be accurate. What's more, the two are not the same, and the other is the same, one is an underground worker, which cannot be compared. However, Justin has just updated another blog, analyzed the test again, and added the test case, which is quite good and worth looking.

The Dynamic lookup feature allows us to use a unified method to dynamically invoke other resources. Through this feature, our objects do not need to be concerned with COM, ironpython, or reflection, we only need to apply the required things to it (friends who are familiar with JavaScript will understand dynamic's idea when they see apply) at runtime. net automatically helps us deal with the rest of the work.

This gives us great flexibility for our development, but dynamic object is closed in the compilation process. Therefore, it is quite troublesome for us to pass the code correctly during the runtime.

From the above reflect code, we can see that the dynamic type can be considered as a special version of the object-only marking that this object can be dynamically used, any object can be converted to the dynamic type when it is displayed. On the contrary, there is also an assignment conversion (Value assignment conversion) that can convert the dynamic type to another type:

Dynamic d = 7; // implicit conversion
Int I = D; // assignment Conversion

Of course, not only can this method be used, but fields, attributes, indexers, operators, and delegation can all be dynamic:

Dynamic d = getdynamicobject (...);
D. M (7); // calling methods
D. F = D. P; // getting and settings fields and Properties
D ["one"] = d ["two"]; // getting and setting through indexers
Int I = d + 3; // calling Operators
String S = D (5, 7); // invoking as a delegate

For the runtime lookup, the dynamic mechanism is determined based on its target object, that is, what new is, it uses this stuff to determine how to lookup and treats com through idispatch, for dynamic objects, idynamicobject is used, for standard. net objects are reflected, which can be easily understood from Microsoft's official documentation.

Let's take a Microsoft example to illustrate a dynamic application:

Let's write an ironpython method:

Code
1 Def Welcome (name ):
2 Return   " Hello' "   + Name +   " 'From fanweixiao "  

Saved as helloworld. py

Then use scriptruntime py = python. createruntime (); create the ironpython runtime environment, with dynamic helloworld = py. usefile ("helloworld. PY "); to create a dynamic type helloworld, you can use helloworld. welcome ("cnblogs guy"); to print the result.

Nice, right? You don't have to stare at a language for Silverlight.

C # The comparison with Java gradually gained the upper hand from the 2.0 generic type, to the 3.0 era of LINQ, and then to the 4.0 era of dynamic, I have to say that the advantage is remarkable. However, technology is always a tool, and tools are born to be used. I agree with Dr. Feng that using existing technologies is sufficient to meet the needs of the current market. No matter how fast the development speed is and how well the product architecture is, we should leave the code to see what our users are thinking. Don't use "I'm doing push-ups" in addition to coding to answer users' questions.

C #4.0 one of the new features: Dynamic lookup (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.