asp.net the use of dynamic in C #

Source: Internet
Author: User
Tags object model reflection

In operations implemented by the dynamic type, the role of the type is to bypass compile-time type checking and resolve these operations at run time instead. This type simplifies access to COM APIs (such as Office automation APIs), dynamic APIs (such as the IronPython Library), and the HTML Document Object Model (DOM).

In most cases, the dynamic type is the same as the behavior of type object. However, operations that contain dynamic type expressions are not parsed or type-checked by the compiler. The compiler packs information about the operation together, and the information is later used to calculate the run-time operation. In this procedure, a variable of type dynamic is compiled into a variable of type object. Therefore, type dynamic only exists at compile time and does not exist at run time.

The following example contrasts a variable of type dynamic with a variable of type object. To validate the type of each variable at compile time, place the mouse pointer over the dyn or obj in the WriteLine statement. IntelliSense shows the "Dyn" and obj "objects" for the.

Class Program
{
static void Main (string[] args)
{
Dynamic dyn = 1;
Object obj = 1;

Rest the mouse pointer over dyn and obj to the their
Types at compile time.
System.Console.WriteLine (Dyn.gettype ());
System.Console.WriteLine (Obj.gettype ());
}
}


The WriteLine statement displays the Run-time type of dyn and obj. At this point, both have the same integer type. The following output will be generated:

System.Int32

System.Int32

To see the difference between dyn and obj, add the following two lines between the declaration and the WriteLine statement in the previous example.

Copy dyn = dyn + 3;
obj = obj + 3;
The compiler error was reported for an attempt to add an integer and object in expression obj + 3. However, dyn + 3 errors are not reported. An expression containing dyn is not checked at compile time because the type of dyn is dynamic.

The dynamic variable is an object variable, which can be validated by the IL code, where the IL code is no longer posted. Of course, the compiler also handles dynamic declarations to distinguish between direct object variables.

Dynamic is being made up by MSDN for ease of interoperability, and I feel it is based on this that some developers misunderstand that because many developers do not touch code such as COM + or office two development, there is a compelling need for a dynamic application reason. So, in daily development, I think dynamic is a valuable point:

The dynamic variable is an object variable, which can be validated by the IL code, where the IL code is no longer posted. Of course, the compiler also handles dynamic declarations to distinguish between direct object variables.
Dynamic is being made up by MSDN for ease of interoperability, and I feel it is based on this that some developers misunderstand that because many developers do not touch code such as COM + or office two development, there is a compelling need for a dynamic application reason. So, in daily development, I think dynamic is a valuable point:

The dynamic variable is an object variable, which can be validated by the IL code, where the IL code is no longer posted. Of course, the compiler also handles dynamic declarations to distinguish between direct object variables. Dynamic is being made up by MSDN for ease of interoperability, and I feel it is based on this that some developers misunderstand that because many developers do not touch code such as COM + or office two development, there is a compelling need for a dynamic application reason. So, in daily development, I think dynamic is a valuable point:

Dynamic can simplify reflection.

We used reflection in this way before:

public class Dynamicsample
{
public string name {get; set;}

public int Add (int a, int b)
{
return a + B;
}
}
Dynamicsample dynamicsample = new Dynamicsample (); Create instance I didn't use reflection to simplify the demo
var addmethod = typeof (Dynamicsample). GetMethod ("add");
int re = (int) addmethod.invoke (dynamicsample, new object[] {1, 2});

Now, we have a simplified version of the wording:

Dynamic dynamicsample2 = new Dynamicsample ();
int Re2 = Dynamicsample2.add (1, 2);


We might disagree with this simplification, after all, it looks like the code hasn't diminished much, but if you take into account the two features of efficiency and elegance, the advantage of dynamic is apparent.

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.