Last active
October 23, 2019 22:57
-
-
Save andanteyk/a471d9494558ef41ec7fa64623f548ae to your computer and use it in GitHub Desktop.
属性持ちの引数なしインスタンスメソッドを探して呼び出すテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]