diff --git a/SecretAPI/Attribute/CallOnLoadAttribute.cs b/SecretAPI/Attribute/CallOnLoadAttribute.cs index 7c02d9a..cb55356 100644 --- a/SecretAPI/Attribute/CallOnLoadAttribute.cs +++ b/SecretAPI/Attribute/CallOnLoadAttribute.cs @@ -4,12 +4,13 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; + using SecretAPI.Features; /// /// Defines the attribute for methods to call on load. /// [AttributeUsage(AttributeTargets.Method)] - public class CallOnLoadAttribute : Attribute + public class CallOnLoadAttribute : Attribute, IPriority { /// /// Initializes a new instance of the class. @@ -32,9 +33,25 @@ public CallOnLoadAttribute(int priority = 0) public static void Load(Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); + CallAttributeMethodPriority(assembly); + } + + /// + public override bool Equals(object? obj) => obj == this; + /// + public override int GetHashCode() => base.GetHashCode(); + + /// + /// Calls the method associated with a Attribute implementing . + /// + /// The assembly to handle calls from. + /// The attribute required for this to be called. + internal static void CallAttributeMethodPriority(Assembly assembly) + where TAttribute : Attribute, IPriority + { const BindingFlags methodFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; - Dictionary methods = new(); + Dictionary methods = new(); // get all types foreach (Type type in assembly.GetTypes()) @@ -42,7 +59,7 @@ public static void Load(Assembly? assembly = null) // get all static methods foreach (MethodInfo method in type.GetMethods(methodFlags)) { - CallOnLoadAttribute? attribute = method.GetCustomAttribute(); + TAttribute? attribute = method.GetCustomAttribute(); if (attribute == null) continue; @@ -50,14 +67,8 @@ public static void Load(Assembly? assembly = null) } } - foreach (KeyValuePair method in methods.OrderBy(static v => v.Key.Priority)) + foreach (KeyValuePair method in methods.OrderBy(static v => v.Key.Priority)) method.Value.Invoke(null, null); } - - /// - public override bool Equals(object? obj) => obj == this; - - /// - public override int GetHashCode() => base.GetHashCode(); } } \ No newline at end of file diff --git a/SecretAPI/Attribute/CallOnUnloadAttribute.cs b/SecretAPI/Attribute/CallOnUnloadAttribute.cs index 2303047..b886d55 100644 --- a/SecretAPI/Attribute/CallOnUnloadAttribute.cs +++ b/SecretAPI/Attribute/CallOnUnloadAttribute.cs @@ -1,15 +1,14 @@ namespace SecretAPI.Attribute { using System; - using System.Collections.Generic; - using System.Linq; using System.Reflection; + using SecretAPI.Features; /// /// Defines the attribute for methods to call on unload. /// [AttributeUsage(AttributeTargets.Method)] - public class CallOnUnloadAttribute : Attribute + public class CallOnUnloadAttribute : Attribute, IPriority { /// /// Initializes a new instance of the class. @@ -32,26 +31,7 @@ public CallOnUnloadAttribute(int priority = 0) public static void Unload(Assembly? assembly = null) { assembly ??= Assembly.GetCallingAssembly(); - - const BindingFlags methodFlags = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; - Dictionary methods = new(); - - // get all types - foreach (Type type in assembly.GetTypes()) - { - // get all static methods - foreach (MethodInfo method in type.GetMethods(methodFlags)) - { - CallOnUnloadAttribute? attribute = method.GetCustomAttribute(); - if (attribute == null) - continue; - - methods.Add(attribute, method); - } - } - - foreach (KeyValuePair method in methods.OrderBy(static v => v.Key.Priority)) - method.Value.Invoke(null, null); + CallOnLoadAttribute.CallAttributeMethodPriority(assembly); } /// diff --git a/SecretAPI/Features/IPriority.cs b/SecretAPI/Features/IPriority.cs new file mode 100644 index 0000000..f337724 --- /dev/null +++ b/SecretAPI/Features/IPriority.cs @@ -0,0 +1,13 @@ +namespace SecretAPI.Features +{ + /// + /// Handles IPriority. + /// + public interface IPriority + { + /// + /// Gets the current priority. + /// + public int Priority { get; } + } +} \ No newline at end of file