I am using reflection to get property names and values in ASP.NET need some advice on optimization

[亡魂溺海] 提交于 2019-12-24 04:41:28

问题


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?


回答1:


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:

public static class PropertyCache<T>
{
    public static SomeType SomeName { get { return someField; } }
    static PropertyCache() {
        // init someField
    }
}
...
var foo = PropertyCache<Foo>.SomeName;



回答2:


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



来源:https://stackoverflow.com/questions/5668569/i-am-using-reflection-to-get-property-names-and-values-in-asp-net-need-some-advi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!