改进性能反射 - 我应该考虑哪些替代方案? [英] Improving performance reflection - what alternatives should I consider?

查看:26
本文介绍了改进性能反射 - 我应该考虑哪些替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态设置一组对象的值或对象的属性,称之为传输对象.

I need to dynamically set values on a bunch or properties on an object, call it a transmission object.

将在短时间内创建大量这些传输对象并设置其属性.我想避免使用反射.

There will be a fair number of these transmission objects that will be created and have its properties set in a short space of time. I want to avoid the use of reflection.

有其他选择吗?如果是这样,是否有我可以查看的示例实现?

Are there alternatives? If so are there sample implementations I could look at?

推荐答案

使用 Delegate.CreateDelegateMethodInfo 转换为强类型委托.这可以大量提高性能.我有一篇关于这个的博文 带有示例代码.请注意,这只会在您需要多次设置相同的属性时有所帮助 - 基本上这意味着在您创建委托时会一次完成许多类型检查,而不是在每次调用时完成.

Use Delegate.CreateDelegate to turn a MethodInfo into a strongly-typed delegate. This can improve performance massively. I have a blog post about this with sample code. Note that this is only going to help if you need to set the same properties multiple times - basically it means that a lot of the type checking is done once when you create the delegate, rather than on every invocation.

Marc Gravell 有一个 HyperPropertyDescriptor 项目,它实现了更好的性能,但引入了额外的依赖.这个项目成为更现代的Fast Member(github).通常,您会使用 Fast Member 而不是 HyperProperty.

Marc Gravell has a HyperPropertyDescriptor project which achieves even better performance, but introduces an extra dependency. This project became the jumping off point for the more modern Fast Member (github). In general you would use Fast Member over HyperProperty.

这篇关于改进性能反射 - 我应该考虑哪些替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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