Using Generics to Create HtmlHelper Extension Methods

前端 未结 2 662
北海茫月
北海茫月 2021-01-23 11:02

I\'m not really familiar with creating generic methods, so I thought I\'d put this question to the community & see what comes back. It might not even be a valid use of gener

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-23 11:09

    I'm thinking generics is not exactly what you are looking for here.

    Instead you might want to take one of two different approaches

    1. Make all of your classes descend from a common one which has a virtual method called "Render" or something similiar. That way you can just call the classes render method.

    2. Make all of your classes implement an Interface which has a Render method..

    That you you would just call:

    Although each class then has to implement it's own Render method, you would 1. avoid reflection, 2. have complete control on a class basis on what the output is.

提交回复
热议问题