Speeding up Reflection API with delegate in .NET/C#(在 .NET/C# 中使用委托加速反射 API)
问题描述
这个帖子有评论如果你需要打电话该方法多次,使用反射一次找到它,然后将其分配给委托,然后调用委托..
- 这个
delegate如何以及为什么工作得更快?谁能举个例子? - 我可以称之为
caching吗?如果是这样,除了这个带委托的缓存方法之外,还有其他方法吗?
- How and why does this
delegatework 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 .NET/C# 中使用委托加速反射 API
基础教程推荐
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
