我正在使用反射来获取 ASP.NET 中的属性名称和值,需要一些优化建议
我使用反射来获取属性(如 {get; set} 属性)名称及其值。我想优化这个反射。我无权访问正在使用反射的客户端类的代码,但在弄清楚所涉及的类的属性名称后,我将一次又一次地重用相同的属性。
我在 ASP.NET 应用程序中执行此操作,因此我正在考虑将一些缓存结果存储在应用程序 (HttpContext.Current.Application) 中,以便第一个用户将主要增加性能负载,但随着请求的扩展,其他用户可以使用第一个用户的缓存结果。
- NHibernate
- Marc Gravell 的 HyperDescripter
有人可以给我一个非常通俗易懂的解释吗 Marc Gravel 的解决方案在这里合适,或者像 NHibernate 这样的东西是否适合这种情况?或者我应该只是缓存通过反射在列表中获取类中属性名称的结果,然后在需要枚举属性名称时使用它?
I am using reflection to get property (as in {get; set} properties) names and their value. I would like to optimize this reflection. I don't have access to the code of the client classes I am using the reflection on, but after figuring out the property names of the class involved I will be reusing the same properties again and again.
I am doing this in an ASP.NET application and so I was thinking of storing some cached results in the Application (HttpContext.Current.Application) so the first user would have the primary increased performance load, but as requests scale other users can use the cached results of the first user.
- NHibernate
- Marc Gravell's HyperDescripter
Can somone give me a very laymans explanation of if Marc Gravel's solution is appropriate here, or if something like NHibernate is good for this situation? Or should I just cache the results of aquiring the property names in the class via reflection in a list and then use that when I need to enumerate property names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HyperDescriptor 是为了适应 ComponentModel API 的特定目的而设计的。如果你不使用它,它可能就太过分了。仅将数据缓存在静态泛型类型中就非常有效,使用静态构造函数来填充数据。我的意思是:存储每个 T 的数据:
HyperDescriptor is designed for the specific purpose of fitting inside the ComponentModel API. If you don't use that, it may be overkill. Just caching the data inside a static generic type can be pretty effective, using a static constructor to populate the data. By which I mean: store the data per-T:
你自己回答了。
“或者我应该只是缓存通过反射在列表中获取类中属性名称的结果,然后在需要枚举属性名称时使用它?”
我使用哈希表实现了这一点。
检查一下:https://stackoverflow.com/a/8038933/497982
You answered yourself.
"Or should I just cache the results of aquiring the property names in the class via reflection in a list and then use that when I need to enumerate property names?"
I implemented this using a hashtable.
Check this: https://stackoverflow.com/a/8038933/497982