From 1d02d9d76d987b69ed08a252a3fa66f7d05a4cbc Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Mon, 4 Sep 2023 02:16:40 +0200 Subject: Clean up some extension functions --- LibMatrix/Extensions/ClassCollector.cs | 22 ------------------ LibMatrix/Extensions/DictionaryExtensions.cs | 32 --------------------------- LibMatrix/Extensions/IEnumerableExtensions.cs | 7 ------ LibMatrix/Extensions/ObjectExtensions.cs | 14 ------------ 4 files changed, 75 deletions(-) delete mode 100644 LibMatrix/Extensions/ClassCollector.cs delete mode 100644 LibMatrix/Extensions/DictionaryExtensions.cs delete mode 100644 LibMatrix/Extensions/IEnumerableExtensions.cs delete mode 100644 LibMatrix/Extensions/ObjectExtensions.cs (limited to 'LibMatrix') diff --git a/LibMatrix/Extensions/ClassCollector.cs b/LibMatrix/Extensions/ClassCollector.cs deleted file mode 100644 index f53850a..0000000 --- a/LibMatrix/Extensions/ClassCollector.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System.Reflection; - -namespace LibMatrix.Extensions; - -public class ClassCollector where T : class { - static ClassCollector() { - if (!typeof(T).IsInterface) - throw new ArgumentException( - $"ClassCollector must be used with an interface type. Passed type: {typeof(T).Name}"); - } - - public List ResolveFromAllAccessibleAssemblies() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(ResolveFromAssembly).ToList(); - - public List ResolveFromObjectReference(object obj) => ResolveFromTypeReference(obj.GetType()); - - public List ResolveFromTypeReference(Type t) => Assembly.GetAssembly(t)?.GetReferencedAssemblies().SelectMany(ResolveFromAssemblyName).ToList() ?? new List(); - - public List ResolveFromAssemblyName(AssemblyName assemblyName) => ResolveFromAssembly(Assembly.Load(assemblyName)); - - public List ResolveFromAssembly(Assembly assembly) => assembly.GetTypes() - .Where(x => x is { IsClass: true, IsAbstract: false } && x.GetInterfaces().Contains(typeof(T))).ToList(); -} diff --git a/LibMatrix/Extensions/DictionaryExtensions.cs b/LibMatrix/Extensions/DictionaryExtensions.cs deleted file mode 100644 index f01cf68..0000000 --- a/LibMatrix/Extensions/DictionaryExtensions.cs +++ /dev/null @@ -1,32 +0,0 @@ -namespace LibMatrix.Extensions; - -public static class DictionaryExtensions { - public static bool ChangeKey(this IDictionary dict, - TKey oldKey, TKey newKey) { - if (!dict.Remove(oldKey, out var value)) - return false; - - dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort - return true; - } - - public static Y GetOrCreate(this IDictionary dict, X key) where Y : new() { - if (dict.TryGetValue(key, out var value)) { - return value; - } - - value = new Y(); - dict.Add(key, value); - return value; - } - - public static Y GetOrCreate(this IDictionary dict, X key, Func valueFactory) { - if (dict.TryGetValue(key, out var value)) { - return value; - } - - value = valueFactory(key); - dict.Add(key, value); - return value; - } -} diff --git a/LibMatrix/Extensions/IEnumerableExtensions.cs b/LibMatrix/Extensions/IEnumerableExtensions.cs deleted file mode 100644 index 8124947..0000000 --- a/LibMatrix/Extensions/IEnumerableExtensions.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace LibMatrix.Extensions; - -[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] -public class MatrixEventAttribute : Attribute { - public string EventName { get; set; } - public bool Legacy { get; set; } -} diff --git a/LibMatrix/Extensions/ObjectExtensions.cs b/LibMatrix/Extensions/ObjectExtensions.cs deleted file mode 100644 index 085de7d..0000000 --- a/LibMatrix/Extensions/ObjectExtensions.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System.Text.Encodings.Web; -using System.Text.Json; - -namespace LibMatrix.Extensions; - -public static class ObjectExtensions { - public static string ToJson(this object obj, bool indent = true, bool ignoreNull = false, bool unsafeContent = false) { - var jso = new JsonSerializerOptions(); - if (indent) jso.WriteIndented = true; - if (ignoreNull) jso.IgnoreNullValues = true; - if (unsafeContent) jso.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping; - return JsonSerializer.Serialize(obj, jso); - } -} -- cgit 1.4.1