<bdo id='Na6IJ'></bdo><ul id='Na6IJ'></ul>

      1. <legend id='Na6IJ'><style id='Na6IJ'><dir id='Na6IJ'><q id='Na6IJ'></q></dir></style></legend>
      2. <small id='Na6IJ'></small><noframes id='Na6IJ'>

        <tfoot id='Na6IJ'></tfoot>

        <i id='Na6IJ'><tr id='Na6IJ'><dt id='Na6IJ'><q id='Na6IJ'><span id='Na6IJ'><b id='Na6IJ'><form id='Na6IJ'><ins id='Na6IJ'></ins><ul id='Na6IJ'></ul><sub id='Na6IJ'></sub></form><legend id='Na6IJ'></legend><bdo id='Na6IJ'><pre id='Na6IJ'><center id='Na6IJ'></center></pre></bdo></b><th id='Na6IJ'></th></span></q></dt></tr></i><div id='Na6IJ'><tfoot id='Na6IJ'></tfoot><dl id='Na6IJ'><fieldset id='Na6IJ'></fieldset></dl></div>
      3. 代表如何工作(在后台)?

        时间:2023-11-11
          <tbody id='S2H1U'></tbody>

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

                <legend id='S2H1U'><style id='S2H1U'><dir id='S2H1U'><q id='S2H1U'></q></dir></style></legend><tfoot id='S2H1U'></tfoot>

                  <bdo id='S2H1U'></bdo><ul id='S2H1U'></ul>

                  <i id='S2H1U'><tr id='S2H1U'><dt id='S2H1U'><q id='S2H1U'><span id='S2H1U'><b id='S2H1U'><form id='S2H1U'><ins id='S2H1U'></ins><ul id='S2H1U'></ul><sub id='S2H1U'></sub></form><legend id='S2H1U'></legend><bdo id='S2H1U'><pre id='S2H1U'><center id='S2H1U'></center></pre></bdo></b><th id='S2H1U'></th></span></q></dt></tr></i><div id='S2H1U'><tfoot id='S2H1U'></tfoot><dl id='S2H1U'><fieldset id='S2H1U'></fieldset></dl></div>
                  本文介绍了代表如何工作(在后台)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  委托在 C# 中如何在幕后工作以及如何有效地使用它们?

                  How do delegates work in c# behind the scenes and how can they be used efficiently?

                  我知道它们在表面上是如何工作的(它们基本上是函数指针,并允许使用它们的地址调用具有某些签名的回调方法).我需要知道的是 CLR 如何在内部实际实现它们.当您定义委托以及使用委托对象调用回调方法时,幕后究竟发生了什么?

                  I know how they work on the surface(they are basically function pointers and allow callback methods with certain signatures to be invoked using their address). What I need to know is how the CLR actually implements them internally. What exactly happens behind the scenes when you define a delegate and when you invoke a callback method using the delegate object?

                  推荐答案

                  Re 效率 - 不清楚你的意思,但它们可以通过避免昂贵的反射来实现效率.例如,通过使用 Delegate.CreateDelegate 为动态/查找方法创建(类型化的)预检查委托,而不是使用(较慢的)MethodInfo.Invoke.

                  Re efficiency - it isn't clear what you mean, but they can be used to achieve efficiency, by avoiding expensive reflection. For example, by using Delegate.CreateDelegate to create a (typed) pre-checked delegate to a dynamic/looked-up method, rather than using the (slower) MethodInfo.Invoke.

                  对于一个简单的示例(访问类型的静态 T Parse(string) 模式),请参见下文.请注意,它只使用一次反射(每种类型),而不是很多次.这应该优于反射或典型的 TypeConverter 用法:

                  For a trivial example (accessing the static T Parse(string) pattern for a type), see below. Note that it only uses reflection once (per type), rather than lots of times. This should out-perform either reflection or typical TypeConverter usage:

                  using System;
                  using System.Reflection;
                  static class Program { // formatted for space
                      static void Main() {
                          // do this in a loop to see benefit...
                          int i = Test<int>.Parse("123");
                          float f = Test<float>.Parse("123.45");
                      }
                  }
                  static class Test<T> {
                      public static T Parse(string text) { return parse(text); }
                      static readonly Func<string, T> parse;
                      static Test() {
                          try {
                              MethodInfo method = typeof(T).GetMethod("Parse",
                                  BindingFlags.Public | BindingFlags.Static,
                                  null, new Type[] { typeof(string) }, null);
                              parse = (Func<string, T>) Delegate.CreateDelegate(
                                  typeof(Func<string, T>), method);
                          } catch (Exception ex) {
                              string msg = ex.Message;
                              parse = delegate { throw new NotSupportedException(msg); };
                          }
                      }
                  }
                  

                  这篇关于代表如何工作(在后台)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:没有 EndInvoke 的 C# 异步调用? 下一篇:如何将代表存储在列表中

                  相关文章

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

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