[C #] CLR via C # Learning Series: Dynamic primitive types and dynamic C #

Source: Internet
Author: User

Today, I read the section of the dynamic primitive type in CLR via C #, just as I saw in the candidate area, why is it possible to say that the Java language is a quasi-dynamic language? Article, which says that Java dependency reflection can be referred to as ' quasi-dynamic language ', while C # is a static language.

I'm not going to tell you the conclusion, first look at what is dynamic language.

Refer to the entry of the Interactive encyclopedia:

Dynamic language, to be precise, means that a program can change its structure at run time: New functions can be introduced, existing functions can be deleted, and so on, structural changes. For example, the well-known ECMAScript (JavaScript) is a dynamic language. In addition, such as Ruby, Python and so on are also dynamic languages, and C, C + + and other languages are not dynamic language.

For example, we can write this in javascript:

1 var temp = {}; 2 function () {3       console.log (' Funca invoked ')4}5 temp. Name = ' JavaScript ';

In the above code, the object temp is an anonymous object and does not have a type structure. Then we append a function Funca and a property name to temp dynamically. This can be written because JavaScript is an interpreted language, and all code is compiled by the interpreter only at run time. The C language, the Java language, and the. Net 3.5 and previous versions of the C # language, are compiled into machine language before running, so all objects must have a type definition at compile time, and the compiler has no way to infer the members of the dynamic schedule and there is no way to compile the code. This type of language is generally referred to as static language.

For a long time, these two kinds of languages have also been peaceful, their own three acres of land. However, the temptation of dynamic programming to programmers is really too big, many engineers will think, we can let the compiler in some way to compile dynamically scheduled objects? The answer is yes. When the. NET CLR does not support dynamic scheduling, engineers have already simulated dynamic scheduling types through CodeDom dynamic compilation techniques and reflection.

Although the performance of this method is very poor, it is very cumbersome to use, but also for the subsequent implementation of the dynamic primitive type provides a theoretical basis.

As we said earlier, dynamic languages are compiled at runtime, so they can be dynamically dispatched. So we can determine the following points:

1. Dynamic scheduling objects need to be run to determine their structure

2. Dynamic scheduling objects need to be dynamically compiled, not precompiled

3. With dynamic compilation and reflection technology, we can simulate the process of dynamic scheduling in an ugly way

So, if you are dynamically compiling objects that need to be dynamically dispatched in Jitcompiler, can you implement dynamic scheduling in a precompiled language?

In. NET 4.0 and later versions, we can write this:

1  Public voidMain ()2 {3      Dynamictemp =NewSystem.Dynamic.ExpandObject ();4Temp. Funca = () ={5Console.WriteLine ("Funca involved");6      };7Temp. Name ="CSharp Dynamic Dispatch";8}

When the compiler compiles to this code, it does not compile it into machine code, but instead joins payload in the assembly and calls the. NET compiler to dynamically compile when it is run, obtains the current instance of the dynamic object, and accesses its members through the memory operation of IL, making performance much higher than reflection. The implementation is also more elegant.

In this case, we can conclude that C # after CLR version 4.0 has conformed to the definition of all dynamic languages and can be called a dynamic language. Even if you like, you can use C # as a dynamic language completely, although there may be some performance loss in some scenarios, but compared with the reflection, it will be a lot better.

[C #] CLR via C # Learning Series: Dynamic primitive types and dynamic C #

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.