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

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

      2. <tfoot id='al570'></tfoot>

        如何动态创建一个动作&lt;T&gt;在运行时?

        How can I dynamically create an Actionlt;Tgt; at runtime?(如何动态创建一个动作lt;Tgt;在运行时?)
            <tbody id='tLepm'></tbody>
              <bdo id='tLepm'></bdo><ul id='tLepm'></ul>

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

              <tfoot id='tLepm'></tfoot>

                <legend id='tLepm'><style id='tLepm'><dir id='tLepm'><q id='tLepm'></q></dir></style></legend>
                <i id='tLepm'><tr id='tLepm'><dt id='tLepm'><q id='tLepm'><span id='tLepm'><b id='tLepm'><form id='tLepm'><ins id='tLepm'></ins><ul id='tLepm'></ul><sub id='tLepm'></sub></form><legend id='tLepm'></legend><bdo id='tLepm'><pre id='tLepm'><center id='tLepm'></center></pre></bdo></b><th id='tLepm'></th></span></q></dt></tr></i><div id='tLepm'><tfoot id='tLepm'></tfoot><dl id='tLepm'><fieldset id='tLepm'></fieldset></dl></div>
                • 本文介绍了如何动态创建一个动作&lt;T&gt;在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望能够在运行时执行以下等效操作:

                  I want to be able to do the equivalent to the following at runtime:

                  var action = new Action<ANYTHING AT RUNTIME>(obj => Console.WriteLine("Called = " + obj));
                  

                  我知道我需要为 Action 获取正确的类型,但不确定如何使用 Delegate.Create 获取最终位.Type 表示 Action 定义中的 T.

                  I know I need to get the correct type for the Action, but not sure how to get the final bit using Delegate.Create. Type represent T in the Action definition.

                  var actionType = typeof(Action<>).MakeGenericType(Type);
                  var constructor = actionType.GetConstructors()[0];
                  var @delegate = Delegate.CreateDelegate(actionType, <WHAT GOES HERE>);
                  

                  人们似乎缺少的一点是我正在尝试创建一个 Action 实例,其中 T 不能静态指定,因为它是从派生自 Attribute 的类中使用的 - 这意味着 T 可以是任何东西,它不能被定义为泛型定义

                  the point people seem to be missing is I'm trying to create an instance of Action where T can not be specified statically because it is being used from a class derived from Attribute - this means T could be anything and it can not be defines as a generic definition

                  干杯

                  推荐答案

                  如果你知道你需要执行的操作是什么以及如何执行它而不考虑类型(如你的示例),为什么不只创建一个通用方法执行操作并以这种方式创建您的委托?

                  If you know what the operation you need to perform is and how to perform it regardless of type (as in your example) why not just make a generic method that performs the operation and create your delegate that way?

                  class Program
                  {
                      public static void Perform<T>(T value)
                      {
                          Console.WriteLine("Called = " + value);
                      }
                  
                      public static Delegate CreateAction(Type type)
                      {
                          var methodInfo = typeof (Program).GetMethod("Perform").MakeGenericMethod(type);
                          var actionT = typeof (Action<>).MakeGenericType(type);
                          return Delegate.CreateDelegate(actionT, methodInfo);
                      }
                  
                      static void Main(string[] args)
                      {
                          CreateAction(typeof (int)).DynamicInvoke(5);
                          Console.ReadLine();
                      }
                  }
                  

                  这篇关于如何动态创建一个动作&lt;T&gt;在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)
                    <bdo id='J0T8J'></bdo><ul id='J0T8J'></ul>
                    <i id='J0T8J'><tr id='J0T8J'><dt id='J0T8J'><q id='J0T8J'><span id='J0T8J'><b id='J0T8J'><form id='J0T8J'><ins id='J0T8J'></ins><ul id='J0T8J'></ul><sub id='J0T8J'></sub></form><legend id='J0T8J'></legend><bdo id='J0T8J'><pre id='J0T8J'><center id='J0T8J'></center></pre></bdo></b><th id='J0T8J'></th></span></q></dt></tr></i><div id='J0T8J'><tfoot id='J0T8J'></tfoot><dl id='J0T8J'><fieldset id='J0T8J'></fieldset></dl></div>

                        <tfoot id='J0T8J'></tfoot>

                          <tbody id='J0T8J'></tbody>

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

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