Generate columnheaders based on the DisplayName attribute?

落花浮王杯 提交于 2019-12-08 07:12:57

问题


When I generate a view from the List template I notice that the names of the columns are not based on the DisplayName() annotation. I know how to edit the list.tt code template but I have no idea how to retrieve the DisplayName attributes from the class properties.


回答1:


The common way to get the DisplayName attribute is via reflection. The issue your going to have is .tt templates and reflection don't play nice together. Reflection relies on code being loaded into the AppDomain. Since .tt files don't actually load code you can't reflect over them.

More information about this issue, and a possible solution here:

http://www.olegsych.com/2007/12/how-to-use-t4-to-generate-decorator-classes/

MVC and Visual Studio must use some type of code inspection to generate some of the generated so I'd look along that path, maybe some crazy regex, if your not into solving the reflection issue.



来源:https://stackoverflow.com/questions/3157577/generate-columnheaders-based-on-the-displayname-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!