Understanding C #4 dynamic (4)-amazing Clay

Source: Internet
Author: User
Tags orchard cms open source cms

Clay is very similar to ExpandoObject and can be seen as an enhanced version of ExpandoObject. They allow us to build the desired object without defining classes. Compared with ExpandoObject, Clay provides more flexible syntax support. It allows us to write C # code like javascript code, and can also be used to build complex objects at multiple layers. Reading directory: i. Initializing objects in multiple ways II. Building a magic Array III. dynamically adding methods to objects IV. dynamically implementing interfaces 5. Clay application background 6. Clay Sample 1, initialize object 1 in multiple ways. The simplest object is to build and initialize dynamic New = new ClayFactory (); var person = New. person (); person. firstName = "Louis"; person. lastName = "Dejardin"; note that the Person here is not a specific, actually existing class or struct. Without defining the Person class, we construct an object with the FirstName and LastName attributes. 2. Use the indexer to initialize var person = New. person (); person ["FirstName"] = "Louis"; person ["LastName"] = "Dejardin"; 3. initialize var person = New using an anonymous object. person (new {FirstName = "Louis", LastName = "Dejardin"}); 4. initialize var person = New using named parameters. person (FirstName: "Louis", LastName: "Dejardin"); 5. initialize var person = New in chained mode. person (). firstName ("Louis "). lastName ("Dejardin"); read attributes by person. firstNameperson ["FirstName"] p The three above methods of erson. FirstName () All access the FirstName attribute, which are equivalent. Various methods of initializing objects and reading attributes make dynamic more flexible. These are not what ExpandoObject can do. 2. Create a magic Array. We can create a JavaScript-style Array: dynamic New = new ClayFactory (); var people = New. array (New. person (). firstName ("Louis "). lastName ("Dejardin"), New. person (). firstName ("Bertrand "). lastName ("Le Roy"); 1. the constructed Array with the Count Attribute Console. writeLine ("Count = {0}", people. count); 2. you can access the Console through indexes. writeLine ("people [0]. firstName = {0} ", people [0]. firstName); 3. supports foreach traversal. Eople) {Console. writeLine ("{0} {1}", person. firstName, person. lastName);} 4. you can easily add the Array property person to an object. aliases ("bleroy", "BoudinFatal"); here, an Array attribute is added to the dynamic object "person". The attribute name is Aliases. Therefore, Aliases can be replaced with any name, it has no specific meaning. The following code is equivalent to the above: persons. aliases1 (new [] {"bleroy", "BoudinFatal"}); 5. the element type in the Array is dynamic, rather than the normal type. Because the type of the Array element is dynamic, the Array: var people = New. array (New. person (). firstName ("Louis "). lastName ("Dejardin"), "Peter"); 3. Add methods for objects dynamically, just like ExpandoObject. You can also extend the methods for them, you need to add one more (). this may be caused by Clay's support for using () to access object attributes. Copy the code var person = New. pserson (); person. firstName = "Louis"; person. lastName = "Dejardin"; person. sayFullName = new Func <string, string> (x => person. firstName + person. lastName + x); Console. writeLine (person. sayFullName () ("Here! "); Copy Code 4. The dynamic implementation interface assumes that we have defined this interface and created an object with the dynamic type, and this object implements this interface, does this seem to be an incomplete task? Clay can do it! Copy the public interface IPerson {string FirstName {get; set;} string LastName {get; set ;}} dynamic New = new ClayFactory (); var people = New. array (New. person (). firstName ("Louis "). lastName ("Dejardin"), New. person (). firstName ("Bertrand "). lastName ("Le Roy"); IPerson lou = people [0]; var fullName = lou. firstName + "" + lou. lastName; copy Code 5. Clay application background Clay is generated in the Orchard CMS project. Orchard CMS is an open source CMS Based on Asp.net MVC. The ViewModel to be used for cms page rendering is unpredictable and pre-defined. There are no rules available, because you cannot know what data will be displayed on the page I use. Orchard wants to build a scalable and Flexible dynamic Object to solve this problem once and for all. This is Clay's original intention. Clay is an independent open-source project. It is omnipotent and can help you simplify many class definitions and reflect code.

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.