Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine attributehelper with reflection class #191

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
31 changes: 31 additions & 0 deletions addons/yat/docs/classes/Reflection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div align="center">
<h3>Reflection</h1>
<p>A static class supporting the use of reflection.</p>
</div>

### 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<T> (this object obj) |
| static T[] | GetAttributes<T> (this object obj) |
| static bool | HasAttribute<T>(this object obj) |

### Signals

**-**

### Enumerations

**-**
2 changes: 1 addition & 1 deletion addons/yat/src/commands/Extensible.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public partial class Extensible : Node
/// <param name="extension">The extension to register.</param>
public void Register(IExtension extension)
{
if (AttributeHelper.GetAttribute<ExtensionAttribute>(extension)
if (Reflection.GetAttribute<ExtensionAttribute>(extension)
is not ExtensionAttribute attribute
)
{
Expand Down
29 changes: 0 additions & 29 deletions addons/yat/src/helpers/AttributeHelper.cs

This file was deleted.

23 changes: 23 additions & 0 deletions addons/yat/src/helpers/Reflection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,27 @@ public static EventInfo[] GetEvents(this object obj, BindingFlags bindingFlags =

return type.GetEvents(bindingFlags);
}

public static T GetAttribute<T>(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<T>(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<T>(this object obj) where T : Attribute
{
return Attribute.GetCustomAttribute(obj.GetType(), typeof(T)) is not null;
}
}
8 changes: 4 additions & 4 deletions addons/yat/src/interfaces/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public partial interface ICommand

public virtual string GenerateCommandManual()
{
CommandAttribute command = AttributeHelper.GetAttribute<CommandAttribute>(this);
bool isThreaded = AttributeHelper.HasAttribute<ThreadedAttribute>(this);
CommandAttribute command = Reflection.GetAttribute<CommandAttribute>(this);
bool isThreaded = Reflection.HasAttribute<ThreadedAttribute>(this);

if (string.IsNullOrEmpty(command?.Manual)) return "This command does not have a manual.";

Expand All @@ -45,7 +45,7 @@ public virtual string GenerateCommandManual()

public virtual string GenerateArgumentsManual()
{
var attributes = AttributeHelper.GetAttributes<ArgumentAttribute>(this);
var attributes = Reflection.GetAttributes<ArgumentAttribute>(this);

if (attributes is null) return "\nThis command does not have any arguments.";

Expand All @@ -71,7 +71,7 @@ public virtual string GenerateArgumentsManual()

public virtual string GenerateOptionsManual()
{
var attributes = AttributeHelper.GetAttributes<OptionAttribute>(this);
var attributes = Reflection.GetAttributes<OptionAttribute>(this);

if (attributes is null) return "\nThis command does not have any options.";

Expand Down
2 changes: 1 addition & 1 deletion addons/yat/src/interfaces/IExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public partial interface IExtension
public virtual string GenerateExtensionManual(params string[] args)
{
StringBuilder sb = new();
ExtensionAttribute attribute = AttributeHelper.GetAttribute<ExtensionAttribute>(this);
ExtensionAttribute attribute = Reflection.GetAttribute<ExtensionAttribute>(this);

if (string.IsNullOrEmpty(attribute?.Manual))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static AddingResult AddCommand(Type commandType)
if (commandInstance is not ICommand command)
return AddingResult.UnknownCommand;

if (AttributeHelper.GetAttribute<CommandAttribute>(command)
if (Reflection.GetAttribute<CommandAttribute>(command)
is not CommandAttribute attribute)
return AddingResult.MissingAttribute;

Expand Down
12 changes: 6 additions & 6 deletions test/helpers/TestAttributeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private class TestClass { }
public void TestGetAttribute_AttributePresent()
{
var obj = new TestClass();
var attribute = AttributeHelper.GetAttribute<CommandAttribute>(obj);
var attribute = Reflection.GetAttribute<CommandAttribute>(obj);

AssertObject(attribute).IsNotNull();
AssertString(attribute.Name).IsEqual("test");
Expand All @@ -29,7 +29,7 @@ public void TestGetAttribute_AttributePresent()
public void TestGetAttribute_AttributeNotPresent()
{
var obj = new TestClass();
var attribute = AttributeHelper.GetAttribute<OptionAttribute>(obj);
var attribute = Reflection.GetAttribute<OptionAttribute>(obj);

AssertObject(attribute).IsNull();
}
Expand All @@ -38,7 +38,7 @@ public void TestGetAttribute_AttributeNotPresent()
public void TestGetAttribute_AttributesPresent()
{
var obj = new TestClass();
var attributes = AttributeHelper.GetAttributes<ArgumentAttribute>(obj);
var attributes = Reflection.GetAttributes<ArgumentAttribute>(obj);

AssertObject(attributes).IsNotNull();
AssertInt(attributes.Length).IsEqual(3);
Expand All @@ -51,7 +51,7 @@ public void TestGetAttribute_AttributesPresent()
public void TestGetAttribute_AttributesNotPresent()
{
var obj = new TestClass();
var attributes = AttributeHelper.GetAttributes<OptionAttribute>(obj);
var attributes = Reflection.GetAttributes<OptionAttribute>(obj);

AssertArray(attributes).IsEmpty();
}
Expand All @@ -60,7 +60,7 @@ public void TestGetAttribute_AttributesNotPresent()
public void TestHasAttribute_AttributePresent()
{
var obj = new TestClass();
var hasAttribute = AttributeHelper.HasAttribute<CommandAttribute>(obj);
var hasAttribute = Reflection.HasAttribute<CommandAttribute>(obj);

AssertBool(hasAttribute).IsEqual(true);
}
Expand All @@ -69,7 +69,7 @@ public void TestHasAttribute_AttributePresent()
public void TestHasAttribute_AttributeNotPresent()
{
var obj = new TestClass();
var hasAttribute = AttributeHelper.HasAttribute<OptionAttribute>(obj);
var hasAttribute = Reflection.HasAttribute<OptionAttribute>(obj);

AssertBool(hasAttribute).IsEqual(false);
}
Expand Down