C # 4.0 new object ExpandoObject

2010-04-30  来源:本站原创  分类:DotNet  人气:441 

First look at the members of the class:

http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject_members (VS.100). ASPx

ExpandoObject instances can add and remove members at run time. What does that mean? This means that such instances can be dynamically at run time, add and remove members.

One of these new concepts: dynamic language runtime (DLR) (Dynamic Language Runtime), I have little talent and less learning, but also hope that many experts to study under.

ExpandoObject talk about the significance of the dynamic characteristics of it.

We use XML to do the next comparison:

First we create an XML object

XElement  contactXML =
new XElement("Contact",
new XElement("Name",  "Patrick Hines"),
new XElement("Phone", "206-555-0144"),
new  XElement("Address",
new XElement("Street1", "123 Main St"),
new XElement("City", "Mercer Island"),
new XElement("State",  "WA"),
new XElement("Postal", "68042")
)
);

Come take a look at Dynamic objects

dynamic contact = new ExpandoObject();
contact.Name = "Patrick  Hines";
contact.Phone = "206-555-0144";
contact.Address =  new ExpandoObject();
contact.Address.Street = "123 Main St";
contact.Address.City = "Mercer Island";
contact.Address.State =  "WA";
contact.Address.Postal = "68402";

First, we see next dynamic object of a statement: dynamic contact = new ExpandoObject ();

I have not written ExpandoObject contact = new ExpandoObject (), because I used to declare static ExpandoObject type, this object is not to increase membership in the run-time features, so I use the new keyword dynamic.

Second, we can note that I only need to create a child node to create a ExpandoObject instance of an object as a contact member.

So you can easily see the relationship between father and son nodes, more importantly, you can easily access each element.

With linq to XML:

Console.WriteLine((string)contactXML.Element("Address").Element("State"));

With ExpandoObject object:

Console.WriteLine(contact.Address.State);

However, when you have lots of contact object, how to do?

Oh, look at the code:

/ / Use XML mode:

XElement contactsXML =
new XElement("Contacts",
new  XElement("Contact",
new XElement("Name", "Patrick Hines"),
new XElement("Phone", "206-555-0144")
),
new  XElement("Contact",
new XElement("Name", "Ellen Adams"),
new  XElement("Phone", "206-555-0155")
)
);

/ / Use dynamic objects:

dynamic  contacts = new List();
contacts.Add(new ExpandoObject());
contacts[0].Name = "Patrick Hines";
contacts[0].Phone =  "206-555-0144";
contacts.Add(new ExpandoObject());
contacts[1].Name = "Ellen Adams";
contacts[1].Phone =  "206-555-0155";

Let's look at how to use Linq to Object to operate the dynamic bar,

var phones  = from c in (contacts as List)
where c.Name == "Patrick Hines"
select c.Phone;
相关文章
  • C # 4.0 new object ExpandoObject 2010-04-30

    First look at the members of the class: http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject_members (VS.100). ASPx ExpandoObject instances can add and remove members at run time. What does that mean? This means that such instances ca

  • zk5.0.2 annotation object graph based view of the bug 2010-06-04

    zk5.0 annotation object graph based view of the bug For example: the user's department list of all employees <listbox model="@{indexWin.user.dept.users}"> <Listitem self = "@ (each = user)" label = "@ (user.name)" &l

  • Detailed API Object XMLHTTP 2009-10-07

    XMLHttpRequest To provide client server communications with the http protocol Dim HttpReq As New MSXML2.XMLHTTP30 HttpReq.open "GET", "http://localhost/books.xml", False HttpReq.send MsgBox HttpReq.responseText Remarks XmlHttp client o

  • dom object and jquery object 2008-11-12

    Difference dom object: Through the javascript in the geElementsByTagName or getElementById to get the elements of Node, so get dom element is the dom object. Dom object can use the javascript in the method var domObj = docuemnt.getElementById("id&quo

  • List <Object> on a particular object conditions Sort by multiple attributes 2010-03-31

    Today a colleague asked me how to object to a List <Object> conditions for dynamic multi-sort. More so because he urgently before it only a specific object Object Object of this sort he is from several other related entities to obtain the required a

  • ibatis single object of all operations 2010-04-03

    Beginners ibatis, version ibatis-2.3.4.726.jar, Jar package required: log4j-1.2.15.jar, ojdbc14.jar. Code has been uploaded, the project does not contain ojdbc14.jar, their combined Kazakhstan. Database oracle10, first of all the various operations o

  • jQuery object with the DOM object of the user authentication protocol 2010-05-04

    We often carried out in the installation of a software or user registration will inevitably prompt the user to read and agree to the agreement and other documents. Here, we passed a small example of the DOM object and the jQuery object and their mutu

  • Symbian mobile client SSL reform program research - to create activities based on main thread and the SSL server object interaction model 2010-05-30

    Symbian mobile client SSL research Reform Programme - Create activities based on main thread and the SSL server object interaction model 1 directory 1 Contents 2 2 Overview 3 2.1 Noun 3 2.2 Option 3 Feasibility Analysis 3 4 3.1 Active Objects and Eve

  • Memo jquery dom object with the conversion 2010-06-09

    Two conversion modes to a jQuery object into DOM objects: [index] and. Get (index); (1) jQuery object is a data object, you can [index] way to get the corresponding DOM object. Such as: var $ v =$("# v "); / / jQuery object var v = $ v [0]; / /

  • JAVASCRIPT common event. Object access techniques 2010-06-22

    JAVASCRIPT common events, objects, access to skills Event source object event.srcElement.tagName event.srcElement.type Capture Release event.srcElement.setCapture (); event.srcElement.releaseCapture (); Events button event.keyCode event.shiftKey even

  • Object pool technique analysis of the basic principles and applications (2) 2010-07-02

    Generic object pool implementation Object pool construction and management can follow a variety of ways. The most flexible way is to pool the type of object in the Class than specified in the object pool, that ObjectPoolFactory class to create object

  • dom jQuery object and the object of mutual transformation 2010-07-06

    Just started learning jQuery, may be you could not tell what a moment jQuery object, which is a DOM object. The DOM object much to explain, we have come too much, concentrate on the following jQuery, and the conversion between the two. What is a jQue

  • jQuery1.4.1 code analysis (2, object access, plug-in mechanism) 2010-07-09

    jQuery's core is divided into jQuery core function jQuery object access Data cache Queue control Plug-in mechanism Coexistence of many libraries Before analysis of the core functions, there are too many do not understand. Today analysis of object acc

  • javascript also the most powerful features is the ability to directly access the browser window object and the child objects. 2010-08-21

    javascript also the most powerful features is the ability to directly access the browser window object and the child objects. Browser window consists of three objects: 1, windows object: the browser window itself, it also has a child object ---- loca

  • jQuery-DOM object and the jQuery object and conversion 2010-08-31

    Just started learning jQuery, may be you could not tell what a moment jQuery object, which is a DOM object. The DOM object much to explain, we have come too much, concentrate on the following jQuery, and the conversion between the two. What is a jQue

  • Handling data in a PHP JSON Object 2010-09-08

    Trends data from Twitter Search API in JSON. Grabbing the file using: $jsonurl = "http://search.twitter.com/trends.json"; $json = file_get_contents($jsonurl,0,null,null); $json_output = json_decode($json); How do I work with data from this objec

  • to upgrade oracle10.2.0.1 linux instructions to upgrade to 10.2.0.3 2010-09-28

    To close the database and listener shutdown immediate Then kill all the oracle process ps-ef | grep oracle | grep-v grep | grep-v ef kill -9 process ID And then extract the installation package is unpacked Disk1 folder to install the package into the

  • Flex Shared Object flash cookie 2010-10-03

    Flex Shared Object flash cookie Flash cookie Is not the first time I heard? everyone is familiar cookie is stored in the browser directory of a text file, the maximum amount of information is 4K. Flash cookie similar to the function of the browser co

  • <. NET 4.0 Object-Oriented Programming Talk> extended reading ─ ─ C # Java of the Integer of int challenges 2010-10-25

    This article is my clumsy me ". NET 4.0 object-oriented programming Talk" written by the expansion of new reading material. This involves a number of principles and Java JVM bytecode instructions, we recommend readers interested in reading a boo

  • Jquery DOM object and distinguish between objects 2010-10-30

    jQuery jQuery object is packaged through the DOM object created object. jQuery jQuery object is unique, it can use jQuery in the method. For example: $ ("# Test"). Html () Means: Get ID for the test elements within the html code. One html () is