Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate for an Action< ref T1, T2>

I'm trying to create a delegate of a static method which takes a ref argument. Please don't ask why I'm doing such a cockamamie thing. It's all part of learning how .Net, C#, and reflection work and how to optimize it.

My code is:

    public struct DataRow     {          private double t;         static public void Cram_T(ref DataRow dr, double a_t)         {             dr.t = a_t;         }     }  ''''   Type myType = typeof(DataRow);   MethodInfo my_Cram_T_Method = myType.GetMethod("Cram_T");   var myCram_T_Delegate =           Delegate.CreateDelegate(typeof(Action<DataRow, Double>),                                        my_Cram_T_Method)                                   as Action<DataRow, Double>; 

This gives me a binding error because (I think) the generic action doesn't match the method.

Inspecting the value of Cram_T_Method in the watch window gives

{Void Cram_T(DataRow ByRef, Double)} 

I then tried using the ref keyword in the Action:

  var myCram_T_Delegate =           Delegate.CreateDelegate(typeof(Action<ref DataRow, Double>),                                           my_Cram_T_Method)                                   as Action<ref DataRow, Double>; 

But this won't compile. The C# compiler chokes at the token "ref".

What is the right way to create this delegate?

like image 267
Max Yaffe Avatar asked Jan 08 '10 19:01

Max Yaffe


People also ask

What Is an Action delegate?

Action delegate is an in-built generic type delegate. This delegate saves you from defining a custom delegate as shown in the below examples and make your program more readable and optimized. It is defined under System namespace.

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 the delegate in C#?

A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.

What is the difference between Func and Action delegate?

An Action type delegate is the same as Func delegate except that the Action delegate doesn't return a value. In other words, an Action delegate can be used with a method that has a void return type. It can contain minimum 1 and maximum of 16 input parameters and does not contain any output parameter.


1 Answers

Create your own delegate type:

delegate void MyAction(ref DataRow dataRow, double doubleValue); 

And use MyAction in place of Action<ref DataRow, Double> -- which, as you've noted, doesn't compile.

like image 129
Ben M Avatar answered Sep 22 '22 21:09

Ben M