C # DynamicObject And ExpandoObject

Source: Internet
Author: User

 

I mentioned DynamicObject in the Dynamic keyword, so I flipped through the book, learned it, and made notes for later use. I 'd like to share these basic things with you ..

1. ExpandoObject indicates an object that can be dynamically added or removed at runtime ., This class is in the namespace System. Dynamic, so it must be related to the Dynamic type. What can this class do? Let's take a look at the following example:

Class Program

{

Static void Main ()

{

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 ();

}

}

Copy code

From the above example, it is not difficult to see that ExpandoObject is very useful. It can dynamically add member variables and methods, so that when we write some objects, we do not need to be so troublesome in a new class, you only need to use this class. However, it should be noted that this class is parsed at runtime, which will cause some performance losses, when writing complicated logic, it is not easy to find the problem. Therefore, for simple objects, we can use this class, so we should not use complicated ones.

2. The biggest difference between DynamicObject and ExpandoObject is that we can inherit DynamicObject and decide how to execute dynamic objects on our own. Let's take a look at the initialization function in DynamicObject definition:

Public class DynamicObject: IDynamicMetaObjectProvider

{

Protected DynamicObject ();

}

 

Www.2cto.com

What we can see is that we cannot directly instantiate DynamicObject, which is also the original intention of DynamicObject design. We define the processing of dynamic members during runtime by ourselves. In general, if we only need to process some dynamic attributes for the custom type, we only need to override the TryGetMember and TrySetMember methods.

Class test: DynamicObject

{

 

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

/* The TryGetMember and TrySetMember methods are rewritten here. After inheriting DynamicObject, You can override a lot of TryXXX methods,

This can be viewed automatically. */

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;

}

}

Class Program

{

Static void Main ()

{

Dynamic employee = new test ();

Employee. Name = "xiaobai ";

Employee. Age = "21 ";

Employee. Sex = "N ";

Console. WriteLine ("Name: {0}, Age: {1}, Sex: {2}", employee. Name, employee. Age, employee. Sex );

Console. ReadKey ();

}

}

 

We can also rewrite many other methods in DynamicObject to implement our operations during dynamic runtime. I will not give an example here. By Rewriting the methods in it, we can implement custom operations during dynamic runtime, so that we can implement more dynamic components.

Author: in vain

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.