[Original address] tip/TRICK: Building a tojson () Extension Method Using. Net 3.5
[Original article published on] Monday, October 01,200
Earlier this year, I introduced a new extension feature "Extension Method" in C # and VB through my blog ".
The extension method allows developers to add new methods to the existing common contracts of CLR types without the need to subclass or re-compile the original types. This approach makes it possible for many useful application scenarios (including LINQ ). The extension method can also be used to easily add "syntactic sugar" to the code ".
Over the past few months, I 've been preparing a list of cool extension methods and planning to implement them when I'm free (not sure when... but at least I can have fun with these ideas ). In the preceding example, two extension methods are used to automatically generate a JSON (JavaScript Object Notation) or XML serialized string for any. Net object.
Simple scenario: tojson () Extension Method
Suppose I have a person class defined as follows (Note: I use the new feature of automatic attributes to implement it ):
Next, I can initialize a set of person objects, and then call the tojson () extension method to obtain the JSON string that represents the content of the set. As follows:
This is similar to the built-in. Net Method for calling the tostring () method of the object class-only the generated result is a string in the JSON format of the set content. Then we can use it on the client in the Ajax scenario:
Note: Click the magnifier icon in the debugger to open "text visualizer" to view JSON serialized strings more conveniently:
Next, this string format can be instantiated as a suitable JavaScript Object on the client using JavaScript to represent the content of my set (Note: Asp. net Ajax has a built-in JavaScript library to support these features ).
Tojson Extension Method
It is easy to implement a basic tojson () Extension Method. You only need to use the javascriptserializer class in the system. Web. Script. serialization namespace, and then define two extension methods as shown below. One method is used to serialize the target object graph (object graph). The other method is an overloaded version that allows you to specify the serialization depth (for example: tojson (2) serializes only two levels of depth ).
Note that the above tojson () extension method is only defined for the "object" type -- this means that it can be used for any type in. Net (not limited to collections ). That is to say, we can not only call the. tojson () method for the preceding set, but also call the tojson () method for individual person objects or any other. Net type.
To use the above extension method, you only need to add the following namespace reference at the top of the program:
Then vs 2008 can provide automatic Code Completion and compilation support for any object for these extension methods:
Note: In addition to the javascriptserializer class,. Net 3.5 also contains a newNew system. runtime. serialization. datacontractjsonserializer classClass, you can also use it for JSON serialization/deserialization.
Summary
We hope that the above example will give you an example of using extended methods to encapsulate functions. Next time, let's look at some good tool libraries to provide functions similar to useful extension methods.
I would like to see other suggestions on reusable extension use cases (Please refer to the comments in this post ). Then we can figure out how to create a good codeplex project and bind these methods to a library for use.
I hope this post will be useful to you,
Scott