Dynamic keywords and ExpandoObject objects in c#4.0

Source: Internet
Author: User

We do Web application development programmers, I think we have a lot of JavaScript contact, in JavaScript we often use a function is to create a new object anytime, anywhere, and then you can customize it to define the attributes they need, as the following simple example

Code

< script type = "Text/javascript" >
function GetObject (name) {
var obj = new Object ();
Custom two properties
Obj. name = name;
Obj. Sex = "male";
return obj;
}

function Onbtnclick () {
var retobj = GetObject ("Acridine dragon"); To invoke a custom property
Alert ("Name:" + retobj.) Name + "\ n Sex:" + retobj. SEX);
}
</script>

Called with the following code

< input type = "button" id = "btntest" value = "Custom Attribute" onclick = "onbtnclick ();"/>

When you click the Custom Properties button, you get the corresponding custom property value as we expected.

Once, I for the above convenient usage crazy and excited, these days saw c#4.0 also provided this usage, is excited unceasingly, obtains you already to feel already ripe has been transported in the heart, please forgive my unresponsive and the pace is slow. Today, with a shared heart, give a brief introduction to the keyword Dynamic and Object Expendoobject objects in c#4.0.

My type, my decision, compiler bypass

The biggest feature of dynamic I think is that its type is not determined at run time, which is the biggest difference between it and the static type keyword. If you use the Dynamic keyword to define a variable in your code operation, the compiler does not type it at compile time, allowing it to be interpreted at runtime. 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, but are packaged together in a variable that compiles the variable to type object and the operation information about it, which is then interpreted at run time. Like the following code:

Dynamic objdynamic = objdynamic + 1;//compiled by
Object objobject = Objobject + 1;//compilation does not pass

The reason is that the compiler does not check for the type of dynamic, which is the biggest difference.

The dynamic keyword can be used in a number of situations, such as in a declaration, as a property, a field, an indexer, a parameter, a return value, or a type constraint type, and so on, and the usage is similar to other basic types of keywords, which can be seen in more detail (C # Reference)

To implement a feature like the one above JavaScript, a new ExpandoObject object is added to the c#4.0 in the assembly: System.core (in System.Core.dll) namespace: system.dynamic , his usage is very similar to the use of JavaScript that I have above, let's take a little slack and borrow an example from MSDN:

  

Code

static void Main (string [] args)
{
Dynamic employee, manager;

Employee = new ExpandoObject ()//with dynamic Create a ExpandoObject object//Customize some attributes to this object
Employee. Name = "John Smith";
Employee. Age = 33;

Manager = new ExpandoObject ();
Manager. Name = "Allison Brown";
Manager. Age = 42;
Manager. Teamsize = 10;

Writeperson (manager);
Writeperson (employee);
}
private static void Writeperson (Dynamic person)
{
Console.WriteLine (' {0} is {1} ' years old. '
Person. Name, person. age);//Call property//The following statement causes an exception
If you pass the Employee object.
Console.WriteLine ("Manages {0} people", person. Teamsize);
//Output "John Smith is-years old." Output ' Allison Brown is ' years old. '

And JavaScript is very similar to the usage, from now on, like to serialize some of the information back to the client, such as simple to pass the value of the case, we can like JavaScript is very convenient to use this method, because rarely write articles, coupled with the shallow knowledge, the article written mess, I hope you forgive , this article is just a trigger for more information on MSDN Technical documents ExpandoObject class and dynamic (C # Reference)

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.