在 .NET/C# 中使用委托加速反射 API

编程入门 行业动态 更新时间:2024-10-28 07:26:12
本文介绍了在 .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

本文发布于:2023-11-04 11:23:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   NET   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!