If reflection is inefficient, when is it most appropriate?

前端 未结 7 448
野的像风
野的像风 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 19:48

    The key to keeping reflection from slowing down your program is to not use it inside a loop. If you want to read a property from an object during startup (happens once), use reflection. You want to read a property from a list of 10,000 objects of unknown type, use reflection to get the property getter delegate once (search term: PropertyInfo.GetGetMethod), then call the delegate 10,000 types. There are plenty of examples of this on StackOverflow.

提交回复
热议问题