123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using Avalonia.Controls;
- using MessagePack;
- using MessagePack.Resolvers;
- using ShakerApp.Models;
- using ShakerApp.ViewModels;
- namespace ShakerApp.Tools
- {
- public static class Tools
- {
- public static void CalcStatistics(ref StatisticsViewModel model,ref float value,uint len)
- {
- }
- public static string GetUnit(this Models.TimeDomainType type)
- {
- return (type.GetType()?
- .GetField(type.ToString())?
- .GetCustomAttributes<TimeDomainUnitAttribute>()
- .FirstOrDefault())?.Unit ?? "";
- }
- private static MessagePackSerializerOptions options = TypelessContractlessStandardResolver.Options.WithOmitAssemblyVersion(true);
- public static byte[] GetBytes<T>(this T value)
- {
- return MessagePack.MessagePackSerializer.Serialize(value,options);
- }
- public static T GetValue<T>(this byte[] data)
- {
- return MessagePackSerializer.Deserialize<T>(data,options);
- }
- public static T? ReadConfig<T>(this string configPath)
- {
- try
- {
- return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(System.IO.File.ReadAllText(configPath));
- }
- catch
- {
- }
- return default;
- }
- public static void SaveConfig<T>(this T? value,string configPath)
- {
- value = value ?? default;
- try
- {
- if (System.IO.File.Exists(configPath)) System.IO.File.Delete(configPath);
- System.IO.File.WriteAllText(configPath, Newtonsoft.Json.JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented));
- }
- catch
- {
- }
- }
- static Dictionary<RuntimeTypeHandle, Control> ViewCache = new Dictionary<RuntimeTypeHandle, Control>();
- public static T CreateView<T>(bool cache = true)where T:Control
- {
- if(cache)
- {
- Type t = typeof(T);
- if (ViewCache.TryGetValue(t.TypeHandle, out var control)) return (T)control;
- else
- {
- ViewCache[t.TypeHandle] = Activator.CreateInstance<T>();
- return (T)ViewCache[t.TypeHandle];
- }
- }
- else
- {
- return Activator.CreateInstance<T>();
- }
- }
- public static Control CreateView(Type type, bool cache = true)
- {
- if (cache)
- {
- if (ViewCache.TryGetValue(type.TypeHandle, out var control)) return control;
- else
- {
- ViewCache[type.TypeHandle] = (Control)Activator.CreateInstance(type)!;
- return ViewCache[type.TypeHandle];
- }
- }
- else
- {
- return (Control)Activator.CreateInstance(type)!;
- }
- }
- }
- }
|