委派一个动作<裁判的T1,T2和GT; [英] Delegate for an Action< ref T1, T2>

查看:170
本文介绍了委派一个动作<裁判的T1,T2和GT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造,这需要裁判参数的静态方法的委托。请不要问我为什么做这样的事情繁琐。它是学习如何.NET,C#和反思工作,以及如何优化它的一部分

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.

我的代码是:

    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.

在监视窗口中检查Cram_T_Method的值给出

Inspecting the value of Cram_T_Method in the watch window gives

{Void Cram_T(DataRow ByRef, Double)}

然后我使用ref关键字在行动尝试:

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>;



但是,这不会编译。 C#编译器电抗器在令牌REF。

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

什么是创建委托的正确方法?

What is the right way to create this delegate?

推荐答案

创建自己的委托类型:

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

和使用 MyAction 在<$的C $ C>动作<裁判的DataRow,双方式> - 这,正如您所指出的那样,不编译

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

这篇关于委派一个动作&LT;裁判的T1,T2和GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆