If reflection is inefficient, when is it most appropriate?

前端 未结 7 475
野的像风
野的像风 2020-12-05 19:44

I find a lot of cases where I think to myself that I could use relfection to solve a problem, but I usually don\'t because I hear a lot along the lines of \"don\'t use refle

7条回答
  •  鱼传尺愫
    2020-12-05 20:05

    It is often "fast enough", and if you need faster (for tight loops etc) you can do meta-programming with Expression or ILGenerator (perhaps via DynamicMethod), to make extremely fast code (including some tricks you can't do in C#).

    Reflection is more commonly used for framework/library scenarios, where the library by definition knows nothing about the caller, and must work based on configuration, attributes or patterns.

提交回复
热议问题