问题
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