设置/获取反射的最快方法是什么? [英] What is the Fastest way to set/get using reflection?

查看:90
本文介绍了设置/获取反射的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我现在正在编写一个自定义实体框架,我的实体在其中定义自己的数据成员标记自定义属性。



我的实体继承了一个抽象基类,它知道在运行时使用关于属性的反射来收集信息(而不是必须处理烦人的抽象函数,这些函数会为每个实体产生它们类型...),属性当然可以静态缓存,以避免开销。



尽管如此,我需要支付的最昂贵的价格是设置时并从对象获取值。我想尽量减少这一点。我已经尝试了几种方法,(PropertyInfo,Reflection.Emit,Fasterflect ......),已经看到了一些基准测试结果等等。但是,因为这在我的框架中是一个相当低级的,并且它不会暴露任何接口,我希望它尽可能快,即使代码不会那么漂亮......



谢谢:)

Hey,

I''m writing nowadays a custom entity framework, in which my entities define their own data members marked by custom attributes.

My entities inherit an abstract base class which knows to gather information using reflection about the properties during run-time (instead of having to deal with annoying abstract functions that would yield them for every entity type...), the properties can of course be cached statically, so that overhead is avoided.

Still, The most expensive price I have to pay is when setting and getting values from an object. I would like to minimize that. I''ve already tried several approaches, (PropertyInfo, Reflection.Emit, Fasterflect...), have seen some benchmark results, etc. However, as this is a rather low-level in my framework, and it doesn''t expose any interfaces, I would prefer it would be as fast as possible even if the code will not be as pretty...

Thanks :)

推荐答案

最终, Reflection.Emit 是最好的方法。如果你发现它不合适,你可能会错过一些东西。我不知道你到底想要达到的目的,所以只需要一些提示:



你不必用Emit创建一个完整的单独组件。您可以选择创建单独的方法。您可以使用 System.Reflection.Emit.DynamicMethod http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod.aspx [ ^ ]。



您可以将Emit与缓存结果相结合。考虑这个高度简化的场景:



为什么你需要通过Reflection传递一些类型?只是因为您需要事先为这些类型及其实例创建一些处理, 不可知这些类型。您可以使用一些类型,使用Reflection处理它们,并在内存中创建一些反射的工件。如果可以是一组 DynamicMethod 实例,或者更好的是,基于这些方法委托实例,以及您使用Reflection挖掘的一些其他信息:名称,基本类型等。



创建一个类来存储这样的反射和发射的所有结果,如果是 ReflectionCache 则调用。



所需要的只是一次而不是重复。因此,您需要创建一些使用某个键索引的字典。您可以使用 System.Type 作为密钥。例如,使用 System.Collections.Generic.Dictionary< System.Type,ReflectionCache>



每个您需要使用某种类型的时间,首先在字典中查找反射缓存。如果找到它,请使用它:调用从之前发出的 DynamicType 实例获得的委托实例,依此类推。如果找不到类型(请记住,类型是键),请执行额外的反射和发射。这样,你就达到了目标:只做一次反射和发射。



-SA
Ultimately, Reflection.Emit is the best way to go. If you found that it is unsuitable, you probably missed something. I don''t know what exactly you are trying to achieve, so just some hints:

You don''t have to create a whole separate assembly with Emit. You may choose to create separate methods. You can use System.Reflection.Emit.DynamicMethod, http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod.aspx[^].

You can combine Emit with caching the results. Consider this highly simplified scenario:

Why would you need to pass some types through Reflection at all? Only because you need to create some processing of these types and their instances, agnostic to those types, in advance. You take some types, process them with Reflection, and create some reflected artifacts in memory. If could be a set of DynamicMethod instances, or, better, delegate instances based on such methods, and some other information you dig out with Reflection: names, base types, etc.

Create a class to store all the results of such Reflection and Emit, call if, say, ReflectionCache.

All you need is to do it all once, not repeatedly. So, you need to create some dictionary indexed with some key. You can use System.Type as the key. Use, for example, System.Collections.Generic.Dictionary<System.Type,ReflectionCache>.

Each time you need to work with some type, you first looks for reflection cache in the dictionary. It if is found, use it: invoke delegate instanced obtained from DynamicType instances your emitted before, and so on. If the type is not found (remember, the type is the key), do extra Reflection and Emit. This way, you reach the goal: doing Reflection and Emit only once.

—SA


在这篇文章的答案中有一些有用的建议...... http://stackoverflow.com/questions / 771524 / how-slow-is-reflection [ ^ ]
There are some useful suggestions in the answer to this post ...http://stackoverflow.com/questions/771524/how-slow-is-reflection[^]


简单地在一个字符串的散列表中缓存PropertyInfo(或FieldInfo等)对象 - >财产足够快,对我来说是一个反思很重的问题。
Simply caching PropertyInfo (or FieldInfo etc) objects in a hashtable of string -> property was fast enough for quite a reflection-heavy problem for me.


这篇关于设置/获取反射的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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