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!