Tools.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 byte[] Serialize(Type type,object? value)
  32. {
  33. return MessagePack.MessagePackSerializer.Serialize(type, value, options);
  34. }
  35. public static object? Deserialize(Type type, ReadOnlyMemory<byte> bytes)
  36. {
  37. try
  38. {
  39. return MessagePack.MessagePackSerializer.Deserialize(type, bytes, options);
  40. }
  41. catch
  42. {
  43. return Activator.CreateInstance(type);
  44. }
  45. }
  46. public static T GetValue<T>(this byte[] data)
  47. {
  48. return MessagePackSerializer.Deserialize<T>(data,options);
  49. }
  50. public static T? ReadConfig<T>(this string configPath)
  51. {
  52. try
  53. {
  54. return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(System.IO.File.ReadAllText(configPath));
  55. }
  56. catch
  57. {
  58. }
  59. return default;
  60. }
  61. public static void SaveConfig<T>(this T? value,string configPath)
  62. {
  63. value = value ?? default;
  64. try
  65. {
  66. if (System.IO.File.Exists(configPath)) System.IO.File.Delete(configPath);
  67. System.IO.File.WriteAllText(configPath, Newtonsoft.Json.JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented));
  68. }
  69. catch
  70. {
  71. }
  72. }
  73. static Dictionary<RuntimeTypeHandle, Control> ViewCache = new Dictionary<RuntimeTypeHandle, Control>();
  74. public static T CreateView<T>(bool cache = true)where T:Control
  75. {
  76. if(cache)
  77. {
  78. Type t = typeof(T);
  79. if (ViewCache.TryGetValue(t.TypeHandle, out var control)) return (T)control;
  80. else
  81. {
  82. ViewCache[t.TypeHandle] = Activator.CreateInstance<T>();
  83. return (T)ViewCache[t.TypeHandle];
  84. }
  85. }
  86. else
  87. {
  88. return Activator.CreateInstance<T>();
  89. }
  90. }
  91. public static Control CreateView(Type type, bool cache = true)
  92. {
  93. if (cache)
  94. {
  95. if (ViewCache.TryGetValue(type.TypeHandle, out var control)) return control;
  96. else
  97. {
  98. ViewCache[type.TypeHandle] = (Control)Activator.CreateInstance(type)!;
  99. return ViewCache[type.TypeHandle];
  100. }
  101. }
  102. else
  103. {
  104. return (Control)Activator.CreateInstance(type)!;
  105. }
  106. }
  107. }
  108. }