Skip to content

Repository files navigation

AttributeHelper

猜你在找:README_CN.md

Used to search for attributes usage in the current assembly at compile time.

Now only features InvokeAllMethods and GetAllMethods.

General Usage

  • Static API Only: All methods must be called statically; generic-based dynamic invocation is not supported.

    • Do not:
void TryInvoke<T>() where T : Attribute
{
	try
	{
		AttributeHelper.InvokeAllMethods<T>();
	}
	catch { }
}
  • Current Assembly Only: The search is limited to the current assembly where the helper is invoked.

  • Public/Internal Is Better: The decorated thing and the attribute should be at least internal, or it will be resolved by reflection.

    • Reflection is slower, and may throw exceptions instead of giving compile errors.
  • Cached: most results are already cached.

InvokeAllMethods

Search for any methods that are marked with the specified attribute and invoke them.

Assumes all decorated methods are static, returns void, and have no parameters or generic parameters. (or gives compile error.)

AttributeHelper.InvokeAllMethods<LoadAttribute>();

class LoadAttribute : Attribute;

class Net
{
	[Load]
	internal static void Init()
	{
		Console.WriteLine("Invoked by attribute helper.");
	}
}

GetAllMethods

Search for any methods that are marked with the specified attribute.

var methods = AttributeHelper.GetAllMethods<MakeSyncAttribute>();
...

class MakeSyncAttribute : Attribute;

class Net
{
	int i = 0;
	[MakeSync]
	internal void Advance()
	{
		i++;
	}
}

Releases

Packages

Contributors

Languages