Creating Attribute Aliases?

时光怂恿深爱的人放手 提交于 2019-12-13 10:53:54

问题


I'm trying to create a generic layer between the frameworks I am using and my application's code and have been blocked by a framework's need to decorate my classes with attributes.

Is there a way to be able to somehow map attributes to other attributes?

Example: Class A is decorated with Attribute B At runtime, map Attribute B to Attribute A Class A is seen as decorated by Attribute A throughout the application's life.


回答1:


What you are trying to do sounds like bad design. Attributes are not runtime types, they are in fact just type metadata. Even though some attributes allow you to change their parameters at runtime, that will not change which attribute is applied.

You will find solutions on the internet that suggest using Reflection.Emit. Note that this is a slippery slope and will lead to highly unmaintainable code.

My personal suggestion would be to create your own Class B that is decorated with both Attribute A as well as Attribute B, and then use internal logic to bridge whatever it is that you are wanting to bridge.



来源:https://stackoverflow.com/questions/28177557/creating-attribute-aliases

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