diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f8feec6..e7b55810 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,9 @@ All notable changes to this project will be documented in this file. - WindowMoved signal & IsWindowMoving property to YatWindow. - Implemented functionality for AllowToGoOffScreen property in YatWindow. -- Documentation for YatWindow class. +- Documentation for: + - YatWindow + - Reflection - Script Templates & C# Project Configuration sections to USAGE.md file. ### Changed @@ -17,6 +19,7 @@ All notable changes to this project will be documented in this file. - Moved YatOptions to the YAT.Resources namespace. - Moved CommandData record to the YAT.Types namespace. - Updated script templates. +- Combined AttributeHelper with Reflection class. ### Fixed diff --git a/addons/yat/docs/classes/Reflection.md b/addons/yat/docs/classes/Reflection.md new file mode 100644 index 00000000..5e138350 --- /dev/null +++ b/addons/yat/docs/classes/Reflection.md @@ -0,0 +1,31 @@ +
+

Reflection

+

A static class supporting the use of reflection.

+
+ +### Description + +**Inherits**: N/A + +This class is used to perform reflection actions on objects, e.g., checking whether an object has a given attribute. + +### Properties + +**-** + +### Methods + +| Type | Definition | +| ------------------ | ----------------------------------------------------------------------------- | +| static EventInfo[] | GetEvents (this object obj, BindingFlags bindingFlags = BindingFlags.Default) | +| static T | GetAttribute (this object obj) | +| static T[] | GetAttributes (this object obj) | +| static bool | HasAttribute(this object obj) | + +### Signals + +**-** + +### Enumerations + +**-** diff --git a/addons/yat/src/commands/Extensible.cs b/addons/yat/src/commands/Extensible.cs index 9025621d..0a6f0beb 100644 --- a/addons/yat/src/commands/Extensible.cs +++ b/addons/yat/src/commands/Extensible.cs @@ -17,7 +17,7 @@ public partial class Extensible : Node /// The extension to register. public void Register(IExtension extension) { - if (AttributeHelper.GetAttribute(extension) + if (Reflection.GetAttribute(extension) is not ExtensionAttribute attribute ) { diff --git a/addons/yat/src/helpers/AttributeHelper.cs b/addons/yat/src/helpers/AttributeHelper.cs deleted file mode 100644 index 0db793a9..00000000 --- a/addons/yat/src/helpers/AttributeHelper.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; - -namespace YAT.Helpers; - -public static class AttributeHelper -{ - public static T GetAttribute(this object obj) where T : Attribute - { - if (Attribute.GetCustomAttribute(obj.GetType(), typeof(T)) - is not T attribute - ) return null; - - return attribute; - } - - public static T[] GetAttributes(this object obj) where T : Attribute - { - if (Attribute.GetCustomAttributes(obj.GetType(), typeof(T)) - is not T[] attributes - ) return null; - - return attributes; - } - - public static bool HasAttribute(this object obj) where T : Attribute - { - return Attribute.GetCustomAttribute(obj.GetType(), typeof(T)) is not null; - } -} diff --git a/addons/yat/src/helpers/Reflection.cs b/addons/yat/src/helpers/Reflection.cs index 8b36ab60..003d3ae1 100644 --- a/addons/yat/src/helpers/Reflection.cs +++ b/addons/yat/src/helpers/Reflection.cs @@ -11,4 +11,27 @@ public static EventInfo[] GetEvents(this object obj, BindingFlags bindingFlags = return type.GetEvents(bindingFlags); } + + public static T GetAttribute(this object obj) where T : Attribute + { + if (Attribute.GetCustomAttribute(obj.GetType(), typeof(T)) + is not T attribute + ) return null; + + return attribute; + } + + public static T[] GetAttributes(this object obj) where T : Attribute + { + if (Attribute.GetCustomAttributes(obj.GetType(), typeof(T)) + is not T[] attributes + ) return null; + + return attributes; + } + + public static bool HasAttribute(this object obj) where T : Attribute + { + return Attribute.GetCustomAttribute(obj.GetType(), typeof(T)) is not null; + } } diff --git a/addons/yat/src/interfaces/ICommand.cs b/addons/yat/src/interfaces/ICommand.cs index 9c9d6996..2af07cc6 100644 --- a/addons/yat/src/interfaces/ICommand.cs +++ b/addons/yat/src/interfaces/ICommand.cs @@ -19,8 +19,8 @@ public partial interface ICommand public virtual string GenerateCommandManual() { - CommandAttribute command = AttributeHelper.GetAttribute(this); - bool isThreaded = AttributeHelper.HasAttribute(this); + CommandAttribute command = Reflection.GetAttribute(this); + bool isThreaded = Reflection.HasAttribute(this); if (string.IsNullOrEmpty(command?.Manual)) return "This command does not have a manual."; @@ -45,7 +45,7 @@ public virtual string GenerateCommandManual() public virtual string GenerateArgumentsManual() { - var attributes = AttributeHelper.GetAttributes(this); + var attributes = Reflection.GetAttributes(this); if (attributes is null) return "\nThis command does not have any arguments."; @@ -71,7 +71,7 @@ public virtual string GenerateArgumentsManual() public virtual string GenerateOptionsManual() { - var attributes = AttributeHelper.GetAttributes(this); + var attributes = Reflection.GetAttributes(this); if (attributes is null) return "\nThis command does not have any options."; diff --git a/addons/yat/src/interfaces/IExtension.cs b/addons/yat/src/interfaces/IExtension.cs index 08021bba..9257ba0a 100644 --- a/addons/yat/src/interfaces/IExtension.cs +++ b/addons/yat/src/interfaces/IExtension.cs @@ -14,7 +14,7 @@ public partial interface IExtension public virtual string GenerateExtensionManual(params string[] args) { StringBuilder sb = new(); - ExtensionAttribute attribute = AttributeHelper.GetAttribute(this); + ExtensionAttribute attribute = Reflection.GetAttribute(this); if (string.IsNullOrEmpty(attribute?.Manual)) { diff --git a/addons/yat/src/scenes/registered_commands/RegisteredCommands.cs b/addons/yat/src/scenes/registered_commands/RegisteredCommands.cs index f1889058..b2c5064a 100644 --- a/addons/yat/src/scenes/registered_commands/RegisteredCommands.cs +++ b/addons/yat/src/scenes/registered_commands/RegisteredCommands.cs @@ -46,7 +46,7 @@ public static AddingResult AddCommand(Type commandType) if (commandInstance is not ICommand command) return AddingResult.UnknownCommand; - if (AttributeHelper.GetAttribute(command) + if (Reflection.GetAttribute(command) is not CommandAttribute attribute) return AddingResult.MissingAttribute; diff --git a/test/helpers/TestAttributeHelper.cs b/test/helpers/TestAttributeHelper.cs index 850ca731..3a747314 100644 --- a/test/helpers/TestAttributeHelper.cs +++ b/test/helpers/TestAttributeHelper.cs @@ -19,7 +19,7 @@ private class TestClass { } public void TestGetAttribute_AttributePresent() { var obj = new TestClass(); - var attribute = AttributeHelper.GetAttribute(obj); + var attribute = Reflection.GetAttribute(obj); AssertObject(attribute).IsNotNull(); AssertString(attribute.Name).IsEqual("test"); @@ -29,7 +29,7 @@ public void TestGetAttribute_AttributePresent() public void TestGetAttribute_AttributeNotPresent() { var obj = new TestClass(); - var attribute = AttributeHelper.GetAttribute(obj); + var attribute = Reflection.GetAttribute(obj); AssertObject(attribute).IsNull(); } @@ -38,7 +38,7 @@ public void TestGetAttribute_AttributeNotPresent() public void TestGetAttribute_AttributesPresent() { var obj = new TestClass(); - var attributes = AttributeHelper.GetAttributes(obj); + var attributes = Reflection.GetAttributes(obj); AssertObject(attributes).IsNotNull(); AssertInt(attributes.Length).IsEqual(3); @@ -51,7 +51,7 @@ public void TestGetAttribute_AttributesPresent() public void TestGetAttribute_AttributesNotPresent() { var obj = new TestClass(); - var attributes = AttributeHelper.GetAttributes(obj); + var attributes = Reflection.GetAttributes(obj); AssertArray(attributes).IsEmpty(); } @@ -60,7 +60,7 @@ public void TestGetAttribute_AttributesNotPresent() public void TestHasAttribute_AttributePresent() { var obj = new TestClass(); - var hasAttribute = AttributeHelper.HasAttribute(obj); + var hasAttribute = Reflection.HasAttribute(obj); AssertBool(hasAttribute).IsEqual(true); } @@ -69,7 +69,7 @@ public void TestHasAttribute_AttributePresent() public void TestHasAttribute_AttributeNotPresent() { var obj = new TestClass(); - var hasAttribute = AttributeHelper.HasAttribute(obj); + var hasAttribute = Reflection.HasAttribute(obj); AssertBool(hasAttribute).IsEqual(false); }