diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-04 02:16:40 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-09-04 02:16:40 +0200 |
commit | 1d02d9d76d987b69ed08a252a3fa66f7d05a4cbc (patch) | |
tree | 27314b621fa27045dcb375da9df9fde374d3c69d /LibMatrix | |
parent | Code cleanup (diff) | |
download | LibMatrix-1d02d9d76d987b69ed08a252a3fa66f7d05a4cbc.tar.xz |
Clean up some extension functions
Diffstat (limited to 'LibMatrix')
-rw-r--r-- | LibMatrix/Extensions/ClassCollector.cs | 22 | ||||
-rw-r--r-- | LibMatrix/Extensions/DictionaryExtensions.cs | 32 | ||||
-rw-r--r-- | LibMatrix/Extensions/IEnumerableExtensions.cs | 7 | ||||
-rw-r--r-- | LibMatrix/Extensions/ObjectExtensions.cs | 14 |
4 files changed, 0 insertions, 75 deletions
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<T> where T : class { - static ClassCollector() { - if (!typeof(T).IsInterface) - throw new ArgumentException( - $"ClassCollector<T> must be used with an interface type. Passed type: {typeof(T).Name}"); - } - - public List<Type> ResolveFromAllAccessibleAssemblies() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(ResolveFromAssembly).ToList(); - - public List<Type> ResolveFromObjectReference(object obj) => ResolveFromTypeReference(obj.GetType()); - - public List<Type> ResolveFromTypeReference(Type t) => Assembly.GetAssembly(t)?.GetReferencedAssemblies().SelectMany(ResolveFromAssemblyName).ToList() ?? new List<Type>(); - - public List<Type> ResolveFromAssemblyName(AssemblyName assemblyName) => ResolveFromAssembly(Assembly.Load(assemblyName)); - - public List<Type> 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<TKey, TValue>(this IDictionary<TKey, TValue> 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<X, Y>(this IDictionary<X, Y> 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<X, Y>(this IDictionary<X, Y> dict, X key, Func<X, Y> 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); - } -} |