如何在C#中缓存反射 [英] how to cache reflection in C#
问题描述
你好我对反射相当熟悉,我已经看过很多例子,我知道它是如何工作的,以及我们可以使用它的目的.但是我没有得到任何缓存反射的例子,我也不知道这是什么意思.不知何故,我必须在我正在做的项目中使用反射缓存.
Hello there I am familiar with reflection quite a bit, I have been through loads of examples and I know how it works and for what purpose we can use it. But I didn't get any examples of caching the reflection, neither do I know what does it mean. And somehow I have to use caching of reflection in of the projects that I am doing.
因此,如果有人可以简要解释这个概念并给出一些示例,我将不胜感激,也将不胜感激现有示例的链接.并且还请描述属性的反射及其缓存.提前致谢.
Therefore, I would be obliged if some one can briefly explain this concept as well as give some examples of it, a link to existing examples would also be appreciated. And please also describe the reflection of attributes as well as its caching. Thanks in advance.
问候乌麦尔
推荐答案
你可以像缓存其他任何东西一样缓存它:
You would cache it like you would anything else:
var cache = new Dictionary<Type, IEnumerable<Attribute>>();
// obj is some object
var type = obj.GetType();
var attributes = type.GetCustomAttributes(typeof(MyAttribute), true);
cache.Add(type, attributes);
这篇关于如何在C#中缓存反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!