如何在C#中缓存反射

Oma*_*ayr 0 c# reflection system.reflection

您好,我对反射很熟悉,我已经通过大量的例子,我知道它是如何工作的,以及我们可以使用它的目的.但我没有得到缓存反射的任何例子,我也不知道它是什么意思.不知何故,我必须在我正在做的项目中使用反射缓存.

因此,如果有人可以简单地解释这个概念并给出一些例子,我将不得不承认,现有例子的链接也将受到赞赏.并且还请描述属性的反映及其缓存.提前致谢.

关心Umair

bri*_*ler 5

你会像其他任何东西一样缓存它:

 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);
Run Code Online (Sandbox Code Playgroud)