Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign method to delegate through reflection

Tags:

c#

reflection

I got stuck up while dynamically assigning methods to a delegate instance through reflection. Below is a sample scenario of the situation i am faced with.

class Program
{
    static void Main(string[] args)
    {
        new DynamicDelegateTest().Test();            
    }
}

public class DynamicDelegateTest
{
    public void Test()
    {
        //This is what i target to do through reflection
        ABC objABC1 = new ABC();
        objABC1.Proc = Debugger;
        objABC1.Test("Helloz");

        //Implementing the same code through reflection
        ABC objABC = new ABC();
        MethodInfo MIDebugger = GetType().GetMethod("Debugger", BindingFlags.NonPublic | BindingFlags.Instance);
        FieldInfo MyProc = objABC.GetType().GetField("Proc", BindingFlags.Public | BindingFlags.Instance);

        //This is the point where I got stuck up
        MyProc.SetValue(objABC, MIDebugger);
        objABC.Test("QWERTY");  
    }

    void Debugger(object Tests)
    {
        Console.WriteLine(Tests);
    }
}

public class ABC
{
    public delegate void Delg(object P1);
    public Delg Proc;

    public void Test(object Tst)
    {
        if (Proc != null) Proc(Tst);
    }
}

Please Help.

like image 507
AbrahamJP Avatar asked Jul 12 '10 14:07

AbrahamJP


People also ask

How do you assign a delegate to a function?

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 difference between delegate and method?

A delegate is a method with a parameter and a return type. A delegate is a type that safely encapsulates a method. Delegates are object-oriented, type safe, and secure.

Which type of delegate will you choose if you want to declare a method that accepts an integer as a parameter and returns an integer?

The myDelegate is a user-defined name of the delegate that takes a single integer parameter and returns an integer value. Therefore, the myDelegate references and invokes a method that takes a single integer parameter and returns an integer value.

What is reflection method in C#?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.


1 Answers

You need to use Delegate.CreateDelegate to get the delegate instance, rather than a method-info. For non-static methods this also includes the target instance. In this case:

object del = Delegate.CreateDelegate(MyProc.FieldType, this, MIDebugger);
MyProc.SetValue(objABC, del);
like image 93
Marc Gravell Avatar answered Sep 19 '22 18:09

Marc Gravell