C # Advanced Programming 72 days----dynamicobject and ExpandoObject

Source: Internet
Author: User

DynamicObject and ExpandoObject

1.ExpandoObject represents an object that contains members that can be dynamically added and removed at run time . This class is related to the dynamic type , what this class can do ?

Case :

ExpandoObject This class needs to introduce system.dynamic;

Dynamic Dyneo = new ExpandoObject ();

Dyneo.number = 20;

Dyneo.method = new Func<int, string> ((int i) = {return (i + 20). ToString (); });

Console.WriteLine (Dyneo.number);

Console.WriteLine (Dyneo.method (Dyneo.number));

Console.readkey ();

ExpandoObjectIt's still useful to be tired.,He can add members and methods on the fly,so that we can write some objects,does not need to beNewaclassso troublesome,as long as this class isOKthe,but one thing to keep in mind,This class is parsed at run time.,so we can get some performance damage.,and,the higher the logic complexity of the program,The more difficult it is to find the problem,so,for a Simple object,We can use this class,don't use it if it's complicated ..

2.DynamicObject

The biggest difference between DynamicObject and ExpandoObject is that we can inherit DynamicObject, Decide for yourself how the dynamic object executes . let 's take a look. DynamicObject the initialization function in the definition :

public class Dynamicobject:idynamicobjectprovider

{

protected DynamicObject ();

}

What we can see is that we can not instantiate the dynamicobject directly , which is also the original intention of dynamicobject design. , We have ourselves to decide on the handling of dynamic members at runtime . in general, if our custom type only needs to handle some dynamic properties, we just need to rewrite Trygetmember and the Trysetmember these two methods .

Using System;

Using System.Collections.Generic;

Using System.dynamic;

Using System.Linq;

Using System.Text;

Using System.Threading.Tasks;

Namespace Dynamic and Expando

{

Class Program

{

static void Main (string[] args)

{

Dynamic employee = new Test ();

Employee. Name = "Zhangsan";

Employee. Age = "22";

Employee. Sex = "Man";

Console.WriteLine ("Name: {0}, age = {1}, sex = {2}", employee. Name,employee. Age,employee. SEX);

Console.readkey ();

}

}

public class Test:dynamicobject

{

dictionary<string, object> dic = new dictionary<string, object> ();

/*

* here Rewrite the trygetmember and trysetmember methods , inherit the after the DynamicObject,

* You can override a whole bunch of tryxxx methods.

*/

public override bool Trygetmember (GetMemberBinder binder, out object result)

{

var name = Binder. Name;

Console.WriteLine (" get {0}", name);

return DIC. TryGetValue (name, out result);

}

public override bool Trysetmember (SetMemberBinder Binder, Object value)

{

var name = Binder. Name;

Dic[name] = value;

Console.WriteLine (" write {0} = {1}", Name,value);

return true;

}

}

}

We can also rewrite Many other methods in DynamicObject to implement our dynamic runtime Operations . implement custom actions in our dynamic runtime by overriding the methods inside , so that we can more trend lines own some of the dynamic components .

I am a more verbose person , I think it is necessary to analyze the two methods of rushing down : Trygetmember and trysetmember

Trysetmember method : provides an implementation for operations that set member values . from DynamicObject Class can override this method , to specify dynamic behavior for operations such as setting property values .

Trygetmember method : provides the value of the member that the implemented operation gets . Inheritance DynamicObject class can override this method to develop dynamic behavior and so on to get the value of a property .

Simply put , Trygetmember Get the value , Trysetmember Set the value .

Trygetmember (GetMemberBinder binder, out object result)method to achieve the acquisition of data, Binder. Nameis the name of the property that gets, resultis the property value to get.throughBinder. Namein theDatagets the corresponding property value in the,out to the outside(can seeresultis a outParameters)

Trysetmember (SetMemberBinder Binder, Object value) assigns a value to the existing property .

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Advanced Programming 72 days----dynamicobject and ExpandoObject

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.