Delegate constructor types

E

Edward Diener

The first type to a delegate constructor is obvious. It is an object * .
What is the .NET type of the second argument to the delegate constructor ?
Can I save an object of that type and use that object to construct a
delegate ? As an example in C#:

public delegate void ADelegate();

public class X
{
public void AMethod();
}

// Normally
X myX = new X;
ADelegate myADel = new ADelegate(myX,X.AMethod);
myADel(); // Calls myX.AMethod();

But let's suppose I want to save the actual method to be called, which is
the second argument to a delegate constructor. Is there a .NET type for this
? If there is, can I create an object of that type passing it a method ?

As an example, using the above delegate and class, let's suppose the .NET
type is called System.Method and it takes a method as a parameter to its
constructor. I could then do:

System.Method mObjMethod = new System.Method(X.AMethod);
// And then later
X myX = new X;
ADelegate myADel = new ADelegate(myX,mObjMethod);
myADel(); // Calls myX.AMethod();

Is this possible in some way ?

In C++ the concept of a method type exists as a pointer to a method of a
particular class. It has the form of:

returnValue SomeClass::*SomeMethod(someParameters);

and one can say:

SomeMethod = SomeClass::AMethod;

as long as AMethod was a member function of SomeClass and had the same
signature as SomeMethod.

Why would I want to do this in .NET, you say ? Because sometimes I don't
know until later what object of a certain type I want to use but I know the
method signature I want to call. So if I could create an instance of just a
method, I could pass that to another function which will find the object,
create the deledgate and invoke it. I can't create the delegate and pass
that because I don't immediately know the object, but I do know the method
signature. Finally the method signature might be one of a number of
identical method signatures of the same type for a particular class, but the
eventual code which invokes the delegate wouldn't know or care about this.
 
J

Jon Skeet [C# MVP]

Edward Diener said:
Why would I want to do this in .NET, you say ? Because sometimes I don't
know until later what object of a certain type I want to use but I know the
method signature I want to call. So if I could create an instance of just a
method, I could pass that to another function which will find the object,
create the deledgate and invoke it. I can't create the delegate and pass
that because I don't immediately know the object, but I do know the method
signature. Finally the method signature might be one of a number of
identical method signatures of the same type for a particular class, but the
eventual code which invokes the delegate wouldn't know or care about this.

It sounds like you don't actually need a delegate then - just save a
MethodInfo variable rather than a delegate, and call Invoke() on it
with the appropriate target and parameters. For instance:

using System;
using System.Reflection;

public class Test
{
string name;

public Test(string name)
{
this.name = name;
}

public void ShowName()
{
Console.WriteLine ("My name is {0}", name);
}

static void Main()
{
MethodInfo method = typeof(Test).GetMethod("ShowName",
new Type[]{});

Test t = new Test("Jon");

method.Invoke (t, new object[]{});
}
}
 
E

Edward Diener

Jon said:
It sounds like you don't actually need a delegate then - just save a
MethodInfo variable rather than a delegate, and call Invoke() on it
with the appropriate target and parameters. snipped...

Thanks, the MethodInfo object is what I was looking for. It does seem a bit
of overkill having to use reflection. I was hoping for an easier way of
grabbing the member function as a reference and invoking it by passing data,
ala C++'s pointer to member function. BTW I believe one can say 'new
Type[0]' and 'new object[0]' below instead of 'new Type[]{}' and 'new
object[]{}'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top