Tools.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Avalonia.Controls;
  8. using MessagePack;
  9. using MessagePack.Resolvers;
  10. using ShakerApp.Models;
  11. using ShakerApp.ViewModels;
  12. namespace ShakerApp.Tools
  13. {
  14. public static class Tools
  15. {
  16. public static void CalcStatistics(ref StatisticsViewModel model,ref float value,uint len)
  17. {
  18. }
  19. public static string GetUnit(this Models.TimeDomainType type)
  20. {
  21. return (type.GetType()?
  22. .GetField(type.ToString())?
  23. .GetCustomAttributes<TimeDomainUnitAttribute>()
  24. .FirstOrDefault())?.Unit ?? "";
  25. }
  26. private static MessagePackSerializerOptions options = TypelessContractlessStandardResolver.Options.WithOmitAssemblyVersion(true);
  27. public static byte[] GetBytes<T>(this T value)
  28. {
  29. return MessagePack.MessagePackSerializer.Serialize(value,options);
  30. }
  31. public static T GetValue<T>(this byte[] data)
  32. {
  33. return MessagePackSerializer.Deserialize<T>(data,options);
  34. }
  35. public static T? ReadConfig<T>(this string configPath)
  36. {
  37. try
  38. {
  39. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(System.IO.File.ReadAllText(configPath));
  40. }
  41. catch
  42. {
  43. }
  44. return default;
  45. }
  46. public static void SaveConfig<T>(this T? value,string configPath)
  47. {
  48. value = value ?? default;
  49. try
  50. {
  51. if (System.IO.File.Exists(configPath)) System.IO.File.Delete(configPath);
  52. System.IO.File.WriteAllText(configPath, Newtonsoft.Json.JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented));
  53. }
  54. catch
  55. {
  56. }
  57. }
  58. static Dictionary<RuntimeTypeHandle, Control> ViewCache = new Dictionary<RuntimeTypeHandle, Control>();
  59. public static T CreateView<T>(bool cache = true)where T:Control
  60. {
  61. if(cache)
  62. {
  63. Type t = typeof(T);
  64. if (ViewCache.TryGetValue(t.TypeHandle, out var control)) return (T)control;
  65. else
  66. {
  67. ViewCache[t.TypeHandle] = Activator.CreateInstance<T>();
  68. return (T)ViewCache[t.TypeHandle];
  69. }
  70. }
  71. else
  72. {
  73. return Activator.CreateInstance<T>();
  74. }
  75. }
  76. public static Control CreateView(Type type, bool cache = true)
  77. {
  78. if (cache)
  79. {
  80. if (ViewCache.TryGetValue(type.TypeHandle, out var control)) return control;
  81. else
  82. {
  83. ViewCache[type.TypeHandle] = (Control)Activator.CreateInstance(type)!;
  84. return ViewCache[type.TypeHandle];
  85. }
  86. }
  87. else
  88. {
  89. return (Control)Activator.CreateInstance(type)!;
  90. }
  91. }
  92. }
  93. }