Method Factory - case vs. reflection

后端 未结 4 452
北恋
北恋 2020-12-15 11:17

I came across some code the other day and I wondered if that was the best way to do it. We have a method that takes a string from some web form data a does something to an

4条回答
  •  再見小時候
    2020-12-15 11:45

    Performance shouldn't be your concern unless you profile it and it's a bottleneck. More significant, IMO, is that you lose static type safety and analysis in the reflection version. There's no way at compile-time to check whether those action methods DoX, DOY, etc. are being invoked. This may or may not be an issue for you, but that would be my biggest concern.

    Also, the number of actions is completely irrelevent for performance of the reflection version. GetMethod does not slow down when you have lots of members in your class.

提交回复
热议问题