developpement:dotnet:outils:linqpad:my_extensions
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
developpement:dotnet:outils:linqpad:my_extensions [2015/05/21 21:46] – sgariepy | developpement:dotnet:outils:linqpad:my_extensions [2022/02/02 00:42] (Version actuelle) – modification externe 127.0.0.1 | ||
---|---|---|---|
Ligne 13: | Ligne 13: | ||
http:// | http:// | ||
+ | |||
+ | |||
+ | ====== Fichier complet ====== | ||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | // Write code to test your extensions here. Press F5 to compile and run. | ||
+ | } | ||
+ | |||
+ | public static class MyExtensions | ||
+ | { | ||
+ | |||
+ | #region ---------- JSON Extensions ---------- | ||
+ | // | ||
+ | public static object DumpJson(this object value, string description = null) | ||
+ | { | ||
+ | return GetJsonDumpTarget(value).Dump(description); | ||
+ | } | ||
+ | |||
+ | public static object DumpJson(this object value, string description, | ||
+ | { | ||
+ | return GetJsonDumpTarget(value).Dump(description, | ||
+ | } | ||
+ | |||
+ | public static object DumpJson(this object value, string description, | ||
+ | { | ||
+ | return GetJsonDumpTarget(value).Dump(description, | ||
+ | } | ||
+ | |||
+ | private static object GetJsonDumpTarget(object value) | ||
+ | { | ||
+ | object dumpTarget = value; | ||
+ | //if this is a string that contains a JSON object, do a round-trip serialization to format it: | ||
+ | var stringValue = value as string; | ||
+ | if (stringValue != null) | ||
+ | { | ||
+ | if (stringValue.Trim().StartsWith(" | ||
+ | { | ||
+ | var obj = JsonConvert.DeserializeObject(stringValue); | ||
+ | dumpTarget = JsonConvert.SerializeObject(obj, | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | dumpTarget = stringValue; | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | dumpTarget = JsonConvert.SerializeObject(value, | ||
+ | } | ||
+ | return dumpTarget; | ||
+ | } | ||
+ | #endregion ---------- JSON Extensions ---------- | ||
+ | |||
+ | public static string DumpJsonToFile(this object jsonObject, string directory, string filename) | ||
+ | { | ||
+ | if (!directory.EndsWith(" | ||
+ | { | ||
+ | directory = directory + " | ||
+ | } | ||
+ | |||
+ | string fullFilename = directory + DateTime.Now.ToShortDateTimeString() + " | ||
+ | |||
+ | File.WriteAllText(fullFilename, | ||
+ | |||
+ | return fullFilename; | ||
+ | } | ||
+ | |||
+ | public static string ToShortDateTimeString(this DateTime date) | ||
+ | { | ||
+ | return date.ToString().Substring(2, | ||
+ | } | ||
+ | |||
+ | |||
+ | public static IEnumerable< | ||
+ | { | ||
+ | using (DbCommand cmd = ctx.Connection.CreateCommand()) | ||
+ | { | ||
+ | cmd.CommandText = query; | ||
+ | ctx.Connection.Open(); | ||
+ | using (DbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) | ||
+ | { | ||
+ | while (rdr.Read()) | ||
+ | { | ||
+ | object[] res = new object[rdr.FieldCount]; | ||
+ | rdr.GetValues(res); | ||
+ | yield return res; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | public static IEnumerable ReadFrom(string file) | ||
+ | { | ||
+ | string line; | ||
+ | using (var reader = File.OpenText(file)) | ||
+ | { | ||
+ | while ((line = reader.ReadLine()) != null) | ||
+ | { | ||
+ | yield return line; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | //namespace RapportInspection | ||
+ | //{ | ||
+ | |||
+ | // public class TestAClass { | ||
+ | // public string Name {get; set;} | ||
+ | // public string Numero {get; set;} | ||
+ | // } | ||
+ | |||
+ | public class Utilities | ||
+ | { | ||
+ | public static string GeneratePassword(int length, int numberOfNonAlphanumericCharacters) | ||
+ | { | ||
+ | return System.Web.Security.Membership.GeneratePassword(length, | ||
+ | } | ||
+ | } | ||
+ | //} | ||
+ | |||
+ | class EOF { | ||
+ | </ | ||
+ | |||
+ | |||
+ | ====== fichier original ====== | ||
+ | |||
+ | |||
+ | <code csharp> | ||
+ | void Main() | ||
+ | { | ||
+ | // Write code to test your extensions here. Press F5 to compile and run. | ||
+ | } | ||
+ | |||
+ | public static class MyExtensions | ||
+ | { | ||
+ | // Write custom extension methods here. They will be available to all queries. | ||
+ | |||
+ | } | ||
+ | |||
+ | // You can also define non-static classes, enums, etc. | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ |
developpement/dotnet/outils/linqpad/my_extensions.1432237606.txt.gz · Dernière modification : 2022/02/02 00:43 (modification externe)