代表如何工作(在后台)?

5

vpn free vpn free 本文介绍了代表如何工作(在后台)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! 免费vpn下载

问题描述

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

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

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

I vpn free know how they work on the vpn free surface(they are basically vpn下载 免费vpn 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 免费vpn the scenes 免费vpn下载 when you vpn free define a delegate and when you invoke a callback method 免费vpn using the delegate object?

推荐答案

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

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

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

For a trivial example (accessing the static 免费vpn T Parse(string) pattern for a type), see below. Note that it only uses reflection once (per type), rather than vpn vpn下载 free lots of times. This should out-perform 免费vpn 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...
 免费vpn     免费vpn下载    int i = vpn free Test<int>.Parse("123");
        float f = Test<float>.Parse("123.45");
    }
}
static class Test<T> {
    public static T Parse(string text) { return parse(text); }
  free vpn下载 vpn   static readonly Func<string, T> parse;
    static Test() free vpn {
   vpn下载      try {
  free vpn  免费vpn          MethodInfo method = typeof(T).GetMethod("Parse",
                vpn free BindingFlags.Public | BindingFlags.Static,
    vpn下载      免费vpn下载        null, new Type[] { typeof(string) }, null);
   免费vpn下载          parse = (Func<string, T>) Delegate.CreateDelegate(
         vpn下载        typeof(Func<string, T>), method);
        } catch (Exception ex) {
            string msg = 免费vpn ex.Message;
   免费vpn    vpn下载       parse = delegate { throw new NotSupportedException(msg); };
  免费vpn       }
 免费vpn下载    }
}

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

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate vpn free weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
vpn free 2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
free vpn 2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14