. Net 4.0 FAQ Part 1-DLR

Source: Internet
Author: User
. Net 4.0 FAQ Part 1-DLR


This articleArticleWe will discuss what new features. NET Framework 4.0 provides. Then we will discuss the characteristics of DLR.Dynamic Object and expando object in. We will also create an expando object to see what benefits we can get. Many developers mistakenly think that dynamic objects are used to replace reflection and object types. We will also correct this concept.

What are the important new features of. Net 4.0?

Instead of browsing the lengthy list of. Net 4.0 new features, let's focus on the three most important new features we think.

•WF and WCF 4.0: This is a major change in. Net 4.0. Simplified configuration, discovery, and routing services are introduced in WCF. The core programming model of WF has also been changed, making it easier and more robust. The most important thing is the integration of WCF and WF.

•Dynamic Language Runtime: DLR provides dynamic programming capabilities to. Net 4.0 CLR. We will discuss this in the next part of the article.

•Parallel expansion: This helps parallel computing in multi-core systems .. . Net 4.0 added Plinq in LINQ to support parallel execution .. Net 4.0 introduces the TPL (task parallel Library), which exposes parallel structures such as parallel "for" and "foreach". They can call common methods or delegates.

Next we will detail the new features mentioned above.

What role does DLR play in. Net 4.0?

DLR (Dynamic Language Runtime) is a group of services that provide dynamic programming capabilities to CLR. DLR enables dynamic languages like lisp, JavaScript, PHP, and Ruby to run on. NET Framework.

Programming LanguageIt can be divided into dynamic type language and static type language. In a static type language, you need to specify the object type during compilation or design. The dynamic type language can recognize the object type at runtime. DLR enables CLR to act as a dynamic LanguageCodeThe running host environment.

With DLR, dynamic languages such as Ruby, Python, and javascript can run seamlessly on the CLR. DLR also helps you build the best experience for your favorite Dynamic Language. In this way, your code for interacting with dynamic languages becomes more concise.

DLR is not limited to dynamic languages. You can also use com InterOP binder to call the MS Office component in a more concise way.

One of the major advantages of DLR is that it provides a subsystem for dynamic languages.

Can I talk about the details of the DLR subsystem?

DLR has three basic subsystems:

•Expression Tree: we can use it to express semantics in AST (Abstract syntax tree) mode. DLR dynamically generates Codes Using AST, which can be executed on CLR. Javascript, Ruby, and other dynamic languages can run on the CLR, which is largely the help of the nested Expression Tree.

•Call Stack cache: When you call a dynamic object method, DLR caches all the information of the method calls. In subsequent calls, DLR uses the cached information for fast scheduling.

•Dynamic object interoperability (DOI): The Doi system contains a group of classes used to create dynamic objects. Developers can use these classes to create dynamic objects that can be used in dynamic or static languages.

Next, we will explain the new features in detail.

How can we use Dynamic Language objects and how can we expose a class to dynamic languages?

To create an object using a language supported by DLR, we can use the dynamic keyword. To expose a class to a dynamic language, we can use the expando class.

So if you want to use an object created by dynamic languages such as Python, Ruby, or Javascript, you can use the reference of dynamic to point to this object. If you want to make your class available to dynamic languages, your class must inherit from the expando class. We will see these two examples later.

(Chinese Text: Dynamic Objects help to create objects using dynamic languages. Expando objects help to expose a class to dynamic languages .)

Are there any examples of dynamic objects?

As we have already discussed, dynamic objects help to access objects created by dynamic languages supported by DLR. The dynamic keyword is part of the Doi subsystem.

If you assign an object to a dynamic variable (dynamic X = new someclass (), all calls to the X method and access to the X attribute, and the operator operations on X will be delayed to the runtime, And the compiler will not perform any type checks on X during compilation.

Consider the following code snippet, in which we try to call the Excel method.


// Get the running object of the Excel applicationobject objapp = system. runtime. interopservices. marshal. getactiveobject ("Excel. application "); // invoke the member dynamicallyobject x = objapp. getType (). invokemember ("name", system. reflection. bindingflags. getproperty, null, objapp, null); // finally get the value by type castingmessagebox. show (X. tostring ());


Use the dynamic keyword to complete code with the same function.


// Get the object using dynamic objapp1 = system. runtime. interopservices. Marshal. getactiveobject ("Excel. application"); // call the MessageBox. Show (objapp1.name );


You can clearly find that the syntax of the access attribute is simplified. The invokemember method has a vague meaning and is prone to errors. Using the dynamic keyword, we can see that the code is simplified.

If you try to view the properties of X in Visual Studio, you will be prompted that the value of this expression can be determined only at runtime.

What is the relationship between dynamic, object, and reflection?

Many developers think that the purpose of introducing dynamic is to replace the reflection or object type. In fact, the main purpose of dynamic is to seamlessly access the objects created by dynamic languages in static languages. It is precisely because of this that part of its purpose coincides with the reflection and object types.

Because dynamic can simplify code and has the advantage of caching, it will eventually replace the reflection and object types. However, the original intention of introducing dynamic is definitely not to replace reflection and object types, but to overlap their purposes.


Object/ reflection

A dynamic object is a small feature provided by the DLR engine. We can use it to access objects created in dynamic languages. DLR not only makes the objects created by dynamic languages accessible, but also exposes your classes to dynamic languages.

the reflection and object types are only used to reference unknown types. Reflection and object types cannot help you expose your class to other languages. They are only used to access objects of specific types that are not known at runtime.

the syntax is quite simple.

syntax is a bit difficult.

The cache for method access improves the performance.

There is no cache for access methods.

What are the advantages and disadvantages of dynamic keywords?

PoorProgramEven if you use the best language, you can write very poor code, and even the worst programming language can be used by good programmers. Dynamic keywords are a good tool to reduce complexity, but they are a curse if used improperly.

Dynamic keywords have the following advantages:

•Help you interact with dynamic languages.

•Eliminate chaotic reflection code and simplify Code complexity.

•Cache with access methods can improve performance.


•When used with a strongly typed class, performance may be compromised.

What is an expando object?

The expando object serves the other end of the Interaction. For example, it makes your custom type accessible in dynamic languages. Therefore, you can create an expando class instance and pass it to dynamic languages such as Ruby, JavaScript, and python. The expando object helps you add attributes at runtime. It is an efficient implementation of dynamic attribute packages. To use an expando object, we must first introduce the system. Dynamic namespace. Create an expandoobject object and assign it to a reference declared by dynamic.

Dynamic OBJ = new expandoobject ();

To create a dynamic attribute, you only need to write the attribute name and assign it a value.

OBJ. customername = "some customer name ";

The value is displayed.

MessageBox. Show (obj. customername );

Can we implement our own expando object?

The expando object only adds attributes to a collection. Therefore, you can create your own expando object.

First, we need to inherit the dynamicobject class.

Public class clsmyexpando: dynamicobject



As mentioned above, we need to define a set to store attributes. Therefore, the second step is to create a dictionary object to maintain the attributes in the set.


Public class clsmyexpando: dynamicobject {dictionary items = New Dictionary ();}



Now we can use trygetmember and setgetmember to define our property accessors.


Public class clsmyexpando: dynamicobject {dictionary items = New Dictionary (); Public override bool trygetmember (getmemberbinder binder, out object result) {return items. trygetvalue (binder. name, out result);} public override bool trysetmember (setmemberbinder binder, object Value) {items [binder. name] = value; return true ;}}


Now we can create our own expando Class Object and assign it to the reference of the dynamic class. In the following code snippet, we assign a value to a dynamic attribute named name.

Dynamic OBJ = new clsmyexpando ();

OBJ. Name = "Dynamic Property ";

What are the benefits of using a custom expando object?

Using expando objects can improve performance. If your class has static and dynamic attributes, you can create static attributes in the Custom expando class like the following code. When the static attribute of the object is accessed, it does not call the dictionary member method, which improves the performance. The DLR engine first tries to access the attribute name instead of calling trygetmember and setgetmember.

If you do not need dynamic attributes or interact with dynamic languages, you must avoid using custom expando types. If you need to use dynamic attributes, make sure that you inherit the dynamicobject class.


Public class clsmyexpando: dynamicobject {dictionary items = New Dictionary (); Private string _ name; Public string name {get {return _ name;} set {_ name = value ;}} public override bool trygetmember (getmemberbinder binder, out object result) {return items. trygetvalue (binder. name, out result);} public override bool trysetmember (setmemberbinder binder, object Value) {items [binder. name] = value; return true ;}}



What are idynamicmetaobjectprovider and dynamicmetaobject?

The Dynamic Object implements idynamicmetaobjectprovider and returns dynamicmetaobject. These two types are the core part of interaction between dynamic languages.

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.