Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating delegate from MethodInfo

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;     } } 
like image 994
thecaptain0220 Avatar asked Jun 20 '12 13:06

thecaptain0220


People also ask

How do you call a delegate in C#?

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.

What is MethodInfo C#?

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.

How do you use delegates?

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.


2 Answers

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.)

like image 160
Jon Skeet Avatar answered Sep 22 '22 14:09

Jon Skeet


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)     {      } } 
like image 30
lidqy Avatar answered Sep 18 '22 14:09

lidqy