What exactly is Reflection and when is it a good approach?

前端 未结 11 1699
天命终不由人
天命终不由人 2020-12-25 14:58

What exactly is Reflection? I read the Wikipedia article on this subject and I understand that it is a kind of meta-programming, where the program can modify itself at run-

11条回答
  •  猫巷女王i
    2020-12-25 14:59

    Reflection is (basically) the ability of a program to query for type information that was available to the compiler. So, for example, given the name of a type you can query for the methods it contains. Then for each method you can query for the types of the parameters they take etc etc etc.

    It's useful for runtime configuration where you've got a config file that specifies the behavior of your application. The config may contain the names of concrete types that you should use (as is often the case with IOC containers). Using reflection you can create an instance of this concrete type (via a reflection API) and use it.

提交回复
热议问题