<legend id='UISgv'><style id='UISgv'><dir id='UISgv'><q id='UISgv'></q></dir></style></legend>

  1. <small id='UISgv'></small><noframes id='UISgv'>

    <i id='UISgv'><tr id='UISgv'><dt id='UISgv'><q id='UISgv'><span id='UISgv'><b id='UISgv'><form id='UISgv'><ins id='UISgv'></ins><ul id='UISgv'></ul><sub id='UISgv'></sub></form><legend id='UISgv'></legend><bdo id='UISgv'><pre id='UISgv'><center id='UISgv'></center></pre></bdo></b><th id='UISgv'></th></span></q></dt></tr></i><div id='UISgv'><tfoot id='UISgv'></tfoot><dl id='UISgv'><fieldset id='UISgv'></fieldset></dl></div>
    • <bdo id='UISgv'></bdo><ul id='UISgv'></ul>
    <tfoot id='UISgv'></tfoot>

    1. 代表一个动作<参考 T1,T2>

      Delegate for an Actionlt; ref T1, T2gt;(代表一个动作参考 T1,T2)
      <i id='dkoqd'><tr id='dkoqd'><dt id='dkoqd'><q id='dkoqd'><span id='dkoqd'><b id='dkoqd'><form id='dkoqd'><ins id='dkoqd'></ins><ul id='dkoqd'></ul><sub id='dkoqd'></sub></form><legend id='dkoqd'></legend><bdo id='dkoqd'><pre id='dkoqd'><center id='dkoqd'></center></pre></bdo></b><th id='dkoqd'></th></span></q></dt></tr></i><div id='dkoqd'><tfoot id='dkoqd'></tfoot><dl id='dkoqd'><fieldset id='dkoqd'></fieldset></dl></div>
          <tfoot id='dkoqd'></tfoot>
              <tbody id='dkoqd'></tbody>
              <bdo id='dkoqd'></bdo><ul id='dkoqd'></ul>

            • <legend id='dkoqd'><style id='dkoqd'><dir id='dkoqd'><q id='dkoqd'></q></dir></style></legend>

            • <small id='dkoqd'></small><noframes id='dkoqd'>

                本文介绍了代表一个动作<参考 T1,T2>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试创建一个采用 ref 参数的静态方法的委托.请不要问我为什么要做这样的 cockamamie 事情.这都是学习 .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)}
                

                然后我尝试在 Action 中使用 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 代替 Action<ref DataRow, Double> - 正如您所指出的那样,它不会编译.

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

                这篇关于代表一个动作<参考 T1,T2>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                How to store delegates in a List(如何将代表存储在列表中)
                How delegates work (in the background)?(代表如何工作(在后台)?)
                C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                <i id='ma3KD'><tr id='ma3KD'><dt id='ma3KD'><q id='ma3KD'><span id='ma3KD'><b id='ma3KD'><form id='ma3KD'><ins id='ma3KD'></ins><ul id='ma3KD'></ul><sub id='ma3KD'></sub></form><legend id='ma3KD'></legend><bdo id='ma3KD'><pre id='ma3KD'><center id='ma3KD'></center></pre></bdo></b><th id='ma3KD'></th></span></q></dt></tr></i><div id='ma3KD'><tfoot id='ma3KD'></tfoot><dl id='ma3KD'><fieldset id='ma3KD'></fieldset></dl></div>
                  <tbody id='ma3KD'></tbody>
                  <bdo id='ma3KD'></bdo><ul id='ma3KD'></ul>

                    <small id='ma3KD'></small><noframes id='ma3KD'>

                      • <tfoot id='ma3KD'></tfoot>
                        <legend id='ma3KD'><style id='ma3KD'><dir id='ma3KD'><q id='ma3KD'></q></dir></style></legend>