什么时候使用反射?模式/反模式 [英] When do you use reflection? Patterns/anti-patterns
问题描述
我了解反射 API(在 c# 中),但我不确定我会在什么情况下使用它.有哪些模式 - 使用反射的反模式?
I understand the reflection API (in c#) but I am not sure in what situation would I use it. What are some patterns - anti-patterns for using reflection?
推荐答案
我在 C# 中使用反射的唯一地方是在工厂模式中,在那里我创建对象(在我的例子中是网络侦听器)基于配置文件信息.配置文件提供了程序集的位置、其中类型的名称以及所需的任何其他参数.工厂选择了这些东西并在此基础上创建了监听器.
The only place I've used the Reflection stuff in C# was in factory patterns, where I'm creating objects (in my case, network listeners) based on configuration file information. The configuration file supplied the location of the assemblies, the name of the types within them, and any additional arguments needed. The factory picked this stuff up and created the listeners based on that.
这篇关于什么时候使用反射?模式/反模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!