Skip to content

Instantly share code, notes, and snippets.

@andanteyk
Last active October 23, 2019 22:57
Show Gist options
  • Save andanteyk/a471d9494558ef41ec7fa64623f548ae to your computer and use it in GitHub Desktop.
Save andanteyk/a471d9494558ef41ec7fa64623f548ae to your computer and use it in GitHub Desktop.
属性持ちの引数なしインスタンスメソッドを探して呼び出すテスト
public static void InvokeAttributedMethod<TInstance, TAttribute>(TInstance instance) where TInstance : class where TAttribute : Attribute
{
foreach (var method in typeof(TInstance).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod))
{
var attr = Attribute.GetCustomAttribute(method, typeof(TAttribute));
if (attr == null)
continue;
if (method.GetParameters().Length > 0)
continue;
method.Invoke(instance, null);
}
}
@andanteyk
Copy link
Author

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment