Arkadiusz Kałkus
Arkadiusz Kałkus

Reputation: 18423

Is it a good idea to use reflection in ASP MVC views?

I'm wondering about preparing generic partial view (or HTML helper) which could generate the table based on view model members and its metadata.

It should look similar to this: C# reflection use variable as object.[var]

Is it a good idea or I should forget about it and every time write the code manually because reflection would be too slow?

Are there any tools / add-ons which are able to generate such a code from a view model?

Upvotes: 1

Views: 3744

Answers (1)

Joseph Woodward
Joseph Woodward

Reputation: 9281

There's nothing wrong (per se) with using reflection in your views and I seriously doubt you'd notice any performance issues with it; however, I would always recommend trying to encapsulate the behavior in an HTML helper if possible as there are certain benefits you'll gain from doing it that you otherwise wouldn't, these are:

  • Increased re-usability,
  • Easier to unit test
  • Benefit of compile time checking

Upvotes: 6

Related Questions