views:

722

answers:

6

Hello, I am using reflection class to invoke some methods which are on the some other dll. And one of the methods' parameters are type of delegate.

And I want to invoke this methods by using reflection. So I need to pass function parameters as object array, but I could not find anything about how to convert delegate to object.

Thanks in advance

+4  A: 

A delegate is an object. Just create the expected delegate as you would normally, and pass it in the parameters array. Here is a rather contrived example:

class Mathematician {
 public delegate int MathMethod(int a, int b);

 public int DoMaths(int a, int b, MathMethod mathMethod) {
  return mathMethod(a, b);
 }
}

[Test]
public void Test() {
 var math = new Mathematician();
 Mathematician.MathMethod addition = (a, b) => a + b;
 var method = typeof(Mathematician).GetMethod("DoMaths");
 var result = method.Invoke(math, new object[] { 1, 2, addition });
 Assert.AreEqual(3, result);
}
Matt Howells
Easy Matt, I think you forgot to include an explaining example!
Ruben Bartelink
+1  A: 

Instances of delegates are objects, so this code works (C#3 style) :

Predicate<int> p = (i)=> i >= 42;

Object[] arrayOfObject = new object[] { p };

Hope it helps !

Cédric

Cédric Rup
+1  A: 

Here's an example:

class Program
{
    public delegate void TestDel();

    public static void ToInvoke(TestDel testDel)
    {
        testDel();
    }

    public static void Test()
    {
        Console.WriteLine("hello world");
    }

    static void Main(string[] args)
    {
        TestDel testDel = Program.Test;
        typeof(Program).InvokeMember(
            "ToInvoke", 
            BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static,
            null,
            null,
            new object[] { testDel });
    }
}
Darin Dimitrov
thank you for your reply.I tried in your way, I am passing same delegate which is in the external dll, but there is weird exception that is saying; Argument Exception: The object with the type "namespace.class.delegate" can not coverted to "namespace.class.delegate"
AFgone
A: 

I'm not sure if I understand your question correctly, but you can create a delegate object from a method like this:

public class Test 
{
   public static void Method() {}
}

Then you create a delegate object like this:

Delegate myDelegate = new Delegate(Test.Method);
Philippe Leybaert
A: 

I think this blog post:

C# Reflection - Dealing with Remote Objects

answers your question perfectly.

CraigTP
A: 

you can see a delegate as variable type "function". the delegate describes the parameters and return value for a matching function.

delegate void Foo(int a);  // here a new delegate obj type Foo has been declared

the above example allows 'Foo' to be used as a data type, the only allowed object that can be matched with a variable of type Foo data type is a method with the same signature so:

void MyFunction(int x);    

Foo D = MyFunction; // this is OK

void MyOtherFunction(string x);

Foo D = MyOtherFunction; // will yield an error since not same signature.

Once you have assigned a method to a delegate, you can invoke the method via the delegate:

int n = 1;
D( n );      // or D.Invoke( n );
Anders K.