Hi, everyone. With our deep understanding of vs2010, we are getting more and more familiar with dynamic, and we are not going to write about it any more, but I think there is something to talk about, so let's just talk about it.
I thought dynamic was determined when it was actually runtime. Later I checked that this runtime was on a new runtime DLR from Microsoft and dynamically determined the type to be used for you, instead of the original CLR, you can refer to Microsoft's msdn for details, because the English is not very good, so as not to mislead everyone, so that we will not write details.
These days, I have been thinking about when to use this keyword.ProgramWithout interacting with other COM and APIs, using dynamic will greatly affect the program performance. However, if compared with reflection, the performance is actually at the same level, maybe faster,CodeIt will be simpler and clearer. When interacting with other COM and APIs, dynamic fully demonstrates its advantages, and in some cases, you have no interactive program, using Dynamic also gives you a different experience. The following two parts are simple applications, one is interaction with ironpython, and the other is the use of dynamic in non-interactive programs.
- In the past, we used to interact with ironpython by writing the following code:
First define a py file: helloworld. py
Def welcome (name ): Return "Hello '" + name + "' from ironpython" |
Then we use ironpython. Net to call this method in this file (to facilitate performance testing, I will define two methods)
Static VoidStaticuseironpythonmethod () { VaRRuntime =Python. Createruntime (); VaRScope = runtime. usefile ("Helloworld. py"); Func<Object,Object> Func = scope. getvariable <Func<Object,Object> ("Welcome"); Func ("Staticuseironpythonmethod"); } |
The above code is actually very simple. Create a python rumtime, then call the previously written py file, then obtain the welcome method, and then execute it.
Let's take a look at the code modified with dynamic (Note: ironpython CTP 2.6 For vs2010 must be used; otherwise, it cannot be used.):
Static VoidDynamicironpythonmethod () { VaRRuntim =Python. Createruntime (); DynamicHello = runtim. usefile ("Helloworld. py"); Hello. Welcome ("ASDF"); } |
See? The Code becomes simple and clear, just like defining the webclome method in your code and then calling it, which is very simple. This is the magic of dynamic, making our code and appearance, because everything is done by DLR.
You may think of performance. After testing, the interaction with ironpython will be slow if you use the previous writing method. If you use dynamic, the performance will be slightly improved. I have rewritten the performance counters of Lao Zhao and b0b0. The code and results are as follows:
Using(CodetimerTimer =New Codetimer("Dynamicironpythonmethod")) { Dynamicironpythonmethod (); } Using(CodetimerTimer =New Codetimer("Staticuseironpythonmethod")) { Staticuseironpythonmethod (); } |
Result:
As you can see, the two are actually at the same level, but our code is much more elegant. As for multiple methods, I have not tested them. You can try them if you are free.
I originally planned to write interesting usage of dynamic in general applications today, but I found that a lot of code needs to be prepared because I didn't have a good preparation. I will release it for the moment, and I will release it tomorrow. However, the possible scenarios are as follows:
I use a portal to search for some content, such as products. However, they have different search conditions based on different product types, but the returned content is the same. If your solution is to splice strings and query data in and out of the database, you can skip the next article, because I will be very "fashionable" here to use oo.
It is roughly as follows:
I have two types of products: mobile phones and books. I have defined these two types. These two classes inherit commodity classes at the same time. On the search page, I can search by different conditions, however, the information displayed on the page is actually some attributes of the product class.
This is probably the case. I thought it would soon be written, but I am more and more aware that this should be explained in detail, because we have already met the details of writing and hope it will be helpful to everyone.
Well, I wrote it here today. After six days of work, I can finally relax. Good night ~
PS: I want to enter the second round of youdao. If you want to enter the second round, you can join me. Let's discuss more about it.
RelatedArticle: "My views on the New Keyword dynamic of framework 4.0 (1 )"