This usage series in C # (ii) extending the method to the original type through the This modifier _c# tutorial

Source: Internet
Author: User
Tags json modifier static class tojson

Defines a static class that defines a static method in which the parameter type is preceded by the This modifier to implement a method extension to the parameter type

Examples for example

namespace demo{//The class here must be static class public static class Json {//method is static method//This modifier is a string type that extends the Tojson method for string type
     public static Object Tojson (this string json) {return Json = = null? Null:JsonConvert.DeserializeObject (Json);} The This modifier is type object, which expands the Tojson method public static string Tojson (This object obj) {var timeconverter = new Isoda for type Object
tetimeconverter {DateTimeFormat = "yyyy-mm-dd HH:mm:ss"};
return Jsonconvert.serializeobject (obj, timeconverter); public static string Tojson (This object obj, string datetimeformats) {var timeconverter = new Isodatetimeconverter {Da
Tetimeformat = Datetimeformats};
return Jsonconvert.serializeobject (obj, timeconverter); public static T toobject<t> (This string json) {return Json = null? Default (t): JSONCONVERT.DESERIALIZEOBJECT&L T
T> (Json); public static list<t> tolist<t> (This string json) {return Json = null? null:JsonConvert.DeserializeObje
Ct<list<t>> (Json); } public static DAtatable ToTable (This string json) {return Json = = null? null:jsonconvert.deserializeobject<datatable> (Json);} public static Jobject Tojobject (This string json) {return Json = null?
Jobject.parse ("{}"): Jobject.parse (Json.replace (" "));} public class User {public string ID {get; set;} public string Code {get; set;} public string Name {get; set;}} CLA SS program {static void Main (Stringtry {list<user> users = new List<user>new user{id= "1", code= "Zs", Name= "John"
}, New user{id= "2", code= "ls", name= "Dick"}}; The list is extended to the Tojson method, which is used to convert strings string json = Users.
Tojson (); The string type is extended out of the Tojson method for converting object obj = json.
Tojson (); The string type is extended out of the ToList method for converting the list users = json.
          
          Tolist<user> (); The string type converts the DataTable DataTable Dt=json.
ToTable ();
catch (Exception ex) {Console.WriteLine (ex);} finally {console.readline ();}} }
}

The above is a small part of the introduction of the C # of this usage series (ii) through the This modifier for the original type of extension method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.