You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

58 lines
1.7 KiB

  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace HBLConsole.Service
  9. {
  10. public static class ExpandMethod
  11. {
  12. public static int GetIndex(this bool[] obj, bool value)
  13. {
  14. if (obj == null) return -1;
  15. return Array.FindIndex(obj, p => p == value);
  16. }
  17. public static int GetIndex(this string[] obj, string value)
  18. {
  19. if (obj == null || value == null) return -1;
  20. return Array.FindIndex(obj, p => p == value && p.Length > 0);
  21. }
  22. /// <summary>
  23. /// 保存数据
  24. /// </summary>
  25. public static void Save<T>(this T ot)
  26. {
  27. string outjson = JsonConvert.SerializeObject(ot);
  28. var str = ot.GetType().GenericTypeArguments;
  29. if (str != null && str.Length > 0)
  30. {
  31. File.WriteAllText(LocaPath.GetInstance.Getpath(str[0].Name), outjson);
  32. }
  33. }
  34. /// <summary>
  35. /// 获取保存的数据
  36. /// </summary>
  37. public static void Read(this object ot)
  38. {
  39. var str = ot.GetType().GenericTypeArguments;
  40. if (str != null && str.Length > 0)
  41. {
  42. string pa = LocaPath.GetInstance.Getpath(str[0].Name);
  43. if (File.Exists(pa))
  44. {
  45. string JsonString = File.ReadAllText(pa);
  46. var result = JsonConvert.DeserializeObject<object>(JsonString);
  47. if (result != null) { Json<object>.Data = result; }
  48. }
  49. }
  50. }
  51. }
  52. }