I am currently running into an issue trying to create delegates from MethodInfo
. My overall goal is to look through the methods in a class and create delegates for ones marked with a certain attribute. I am trying to use CreateDelegate
but I am getting the following error.
Cannot bind to the target method because its signature or security transparency is not compatible with that of the delegate type.
Here is my code
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; } }
Delegates can be invoke like a normal function or Invoke() method. Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.
The MethodInfo class represents a method of a type. You can use a MethodInfo object to obtain information about the method that the object represents and to invoke the method.
You can pass methods as parameters to a delegate to allow the delegate to point to the method. Delegates are used to define callback methods and implement event handling, and they are declared using the “delegate” keyword. You can declare a delegate that can appear on its own or even nested inside a class.
You're trying to create a delegate from an instance method, but you're not passing in a target.
You could use:
Delegate.CreateDelegate(typeof(TestDelagate), this, method);
... or you could make your method static.
(If you need to cope with both kinds of method, you'll need to do that conditionally, or pass in null
as the middle argument.)
You need a different signature for the delegate, if it has no target. The target needs to be passed as the first argument then
public class TestClass { public delegate void TestDelagate(TestClass instance, 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), null, method); delegates.Add(newDelegate); //Invocation: newDelegate.DynamicInvoke(this, "hello"); } } } [Test] public void TestFunction(string test) { } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With