Variable Argument Generic Delegate

  • Thread starter Thread starter Advait Mohan Raut
  • Start date Start date
A

Advait Mohan Raut

Hello,
I am using VC# 2005 ; C# 2.0 I am working on the performance
measurement tool. For that I need my code to call user defined method
along with its parameters. For that should I use a generic delegate
with variable length argument or something else ? if it is then how to
use it ?

AA
advait
 
Peter said:
Careful with that word "generic". It has a very specific meaning in C#,
and there's nothing in your post that suggests it's applicable here.

As far as the actual question goes, the Delegate class has the
DynamicInvoke() method, which takes a variable-length parameter list. So,
all delegate instances have this method that can be used when you don't
have compile-time knowledge of the signature of the method.

If that doesn't meet your needs, you'll have to be more specific about
your question, explaining why DynamicInvoke() doesn't work for you and
what exact behavior you think you need.

Pete

Hello,
My class accepts a delegate from the programmer. If I make my class to
accept particual type of delegate then it will create problem when the
methos of diffrent signature is to be passed. If I make my function
just to accept general 'Delegate' then the problem is to create the
instance of the delegate which I would pass.

class TestClass
{
publicTestClass(Delegate d)
{
del=d;
}
public Call(params object[] args)
{
del.InvokeDynamic(args);
}
private Delegate del;
}

int MyMethod(int n)
{
....
}

//Main()

TestClass test=new TestClass(???); // <--- The problem is here to
pass MyMethod


AA
Advait
 
[...]
TestClass test=new TestClass(???);  // <--- The problem is here to
pass MyMethod

Just pass an instance of a concrete delegate created with your method.

You can't make it simply an instance of "Delegate", since that's an  
abstract class.  But any other compatible delegate type will do.

If you're using .NET 3.0, you could use the Func<> generic delegate type  
in this particular example (and numerous other similar ones):

     TestClass test = new TestClass((Func<int, int>)MyMethod);

Otherwise, you could just declare your own delegate type and use that (you  
could even make it generic, just like .NET's own Func<> type).

The Action<> generic delegate type would be useful for methods that return  
void.

I find it intriguing that C# still cannot infer an appropriate anonymous  
delegate type to use in this case.  It knows the signature, and it knows  
the Delegate type is special.  If it could, it would fix these kinds of 
scenarios by making them more elegant, including one very notable one: the  
Control.Invoke() method.

But for now, you need to be explicit about what type you really want (on  
the bright side, once you've defined the precise type, the compiler will  
at least infer the correct object instantiation for you :) ).

Pete

Thank you Peter Duniho for the response.
Since I use C# 2.0 , I don't have any other way except creating the
signatures for the functions.
 
Peter Duniho said:
If you're using .NET 3.0, you could use the Func<> generic delegate type
in this particular example (and numerous other similar ones):

TestClass test = new TestClass((Func<int, int>)MyMethod);

Otherwise, you could just declare your own delegate type and use that (you
could even make it generic, just like .NET's own Func<> type).

The Action<> generic delegate type would be useful for methods that return
void.

Just a slight correction - these types were introduced in .NET 3.5, not
..NET 3.0.
 
I find it intriguing that C# still cannot infer an appropriate anonymous
delegate type to use in this case
...
including one very notable one: the
Control.Invoke() method.

So true; a real frustration at times. As an aside (and not helpful for
Advait with C# 2.0), one option with Control.Invoke is an extension
method:

public static void Invoke(this Control control, Action action)
{ // note: could use MethodInvoker for 2.0 compartibility
(with ExtensionAttribute)
if (control == null) throw new
ArgumentNullException("control");
if (action == null) throw new
ArgumentNullException("action");
control.Invoke(action, null);
}

then you can use:

this.Invoke(() => this.Text = "Hi");

Marc
 
Back
Top