からデリゲートを作成しようとして、現在問題が発生していますMethodInfo
。私の全体的な目標は、クラス内のメソッドを調べて、特定の属性でマークされたメソッドのデリゲートを作成することです。使用しようとしていますCreateDelegate
が、次のエラーが表示されます。
署名またはセキュリティ透過性がデリゲート型のものと互換性がないため、ターゲット メソッドにバインドできません。
これが私のコードです
public class TestClass
{
public delegate void TestDelagate(string test);
private List<TestDelagate> delagates = new List<TestDelagate>();
public TestClass()
{
foreach (MethodInfo method in this.GetType().GetMethods())
{
if (TestAttribute.IsTest(method))
{
TestDelegate newDelegate = (TestDelagate)Delegate.CreateDelegate(typeof(TestDelagate), method);
delegates.Add(newDelegate);
}
}
}
[Test]
public void TestFunction(string test)
{
}
}
public class TestAttribute : Attribute
{
public static bool IsTest(MemberInfo member)
{
bool isTestAttribute = false;
foreach (object attribute in member.GetCustomAttributes(true))
{
if (attribute is TestAttribute)
isTestAttribute = true;
}
return isTestAttribute;
}
}