在 .NET/C# 中使用委托加速反射 API [英] Speeding up Reflection API with delegate in .NET/C#

查看:19
本文介绍了在 .NET/C# 中使用委托加速反射 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个帖子有评论如果你需要打电话该方法多次,使用反射一次找到它,然后将其分配给委托,然后调用委托..

  • 这个 delegate 如何以及为什么工作得更快?谁能举个例子?
  • 我可以称之为 caching 吗?如果是这样,除了这个带委托的缓存方法之外,还有其他方法吗?
  • How and why does this delegate work faster? Can anyone has some example?
  • Can I call this caching? If so, are there any other method than this caching method with delegate?

我想出了一个使用 delegate 这里.

I came up with an example of using delegate here.

推荐答案

A delegate 只是一个指向函数的指针.如果您(完全)使用反射,通常会有很多与之相关的开销.通过一次找到此方法地址并将该地址分配给您的委托变量,您实际上是在缓存它.

A delegate is simply a pointer to a function. If you're using reflection (at all) there is generally a lot of overhead associated with it. By finding this methods address once and assigning that address to your delegate variable, you are in effect caching it.

因此,不是委托"类型工作得更快,只是您计算"一次并多次使用"它可以提高速度.

So, it's not the "delegate" type that works faster, it's just that you're "computing" once and "using" it multiple times that grants you the speed increase.

这篇关于在 .NET/C# 中使用委托加速反射 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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