缓存的反思结果(类属性) [英] Cache Reflection Results (Class Properties)

查看:135
本文介绍了缓存的反思结果(类属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到相当静态的数据不应该被重新评估,但缓存,而不是,我不知道是否有可能使用反射一旦获得类的属性,然后将它们缓存,这样我可以动态评估对象的属性和读/赋值,但不是每次我这样做,有思考的开销。这是可能的(样品code?)?

considering that fairly static data should not be re-evaluated but cached instead, I wondered if it is possible to use Reflection to obtain class properties once, and then cache them so that I could dynamically evaluate object properties and read/assign values, but not have the Reflection overhead every time I do that. Is this possible (Sample code?) ?

要澄清一点,可以说我有这个类:

To clarify a bit, lets say I have this class:

public class Cloud
{
     Boolean IsWhite;
}

和我想现在做的方法,可以让我做这样的事情(伪code):

and I'm trying to now make a method that allows me to do something like this (pseudocode):

Update(myCloudInstance, new {IsWhite, true});

而不是再做思考

更新,现在应该检查缓存第一,如果它知道云(typeof运算(myCloudInstance))已经在性质,然后使用缓存的信息来​​分配财产的字符iswhite值true。

Update should now check with the cache first if it knows already the properties of Cloud (typeof(myCloudInstance)), and then use cached information to assign the property "IsWhite" the value "true" instead of doing Reflection again.

这是如何做到这一点任何想法?

Any ideas on how to do this?

推荐答案

目前尚不清楚的完全的你在做什么,但缓存当然也能与反思的差异。

It's not clear exactly what you're doing, but caching can certainly make a difference with reflection.

在特定的,如果你调用方法(或属性的getter / setter),并且可以在一个类型安全的方式做到这一点,只要调用code而言,它<一个href="http://msmvps.com/blogs/jon%5Fskeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx"相对=nofollow>可以做一个的巨大的影响力如果转换的MethodInfo 成一个强类型的委托一次,然后再利用这一点。

In particular, if you're invoking methods (or property getters/setters) and can do so in a type-safe way as far as the calling code is concerned, it can make a huge difference if you convert the MethodInfo into a strongly-typed delegate once and then reuse that.

如果你可以给我们你想要做一个完整的例子,这将帮助我们拿出更多具体的想法,甚至code。如果你只是要缓存的PropertyInfo ,可能不会有尽可能多的(或)作用 - 它可能是正常的 Type.GetProperty (等)的方法已经快pretty的。与以往一样,性能问题,关键是要衡量一下你实际上在做。再次进行更改和措施等。

If you could give us a complete example of what you're trying to do, that would help us to come up with more specific ideas or even code. If you're just going to cache a PropertyInfo that may not have as much (or any) effect - it's possible that the normal Type.GetProperty (etc) methods are already pretty fast. As ever with performance questions, the key is to measure what you're actually doing. Make a change and measure again, etc.

这篇关于缓存的反思结果(类属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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