-1

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.

  • very difficult to determine exactly what you are doing or trying to do without posting all relevant code.. – MethodMan Jan 27 '15 at 18:40
  • Are the classes `partial`? https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute%28v=vs.110%29.aspx – Paul Abbott Jan 27 '15 at 18:41
  • Maybe [ICustomTypeDescriptor](https://msdn.microsoft.com/en-us/library/system.componentmodel.icustomtypedescriptor%28v=vs.110%29.aspx) but I'm pretty sure this is a classic [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What framework it is? May be that provides some other option which you're not aware of. – Sriram Sakthivel Jan 27 '15 at 18:41
  • @PaulAbbott How that will help? – Sriram Sakthivel Jan 27 '15 at 18:44
  • You won't be able to map attributes, but you will be able to add them. So Class A can get decorated by Attribute A (it will just have Attribute B as well). – Paul Abbott Jan 27 '15 at 18:49
  • http://stackoverflow.com/questions/129285/can-attributes-be-added-dynamically-in-c – Andrew Camilleri 'Kukks' Jan 28 '15 at 18:34

1 Answers1

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.

Stefan Z Camilleri
  • 4,016
  • 1
  • 32
  • 42
  • Yes, I stick to my answer ;) Adding a middle layer between a DI framework and your actual codebase will result in a hard to read (and maintain) architecture. You may want to consider an alternative DI framework in this case, or else see if your DI framework of choice allows you to create custom resolvers. – Stefan Z Camilleri Jan 28 '15 at 23:52
  • 1
    http://stackoverflow.com/questions/3493640/mef-how-to-manually-configure-export-for-a-contract-implementation found what I wanted to do without massacring the architecture :) – Andrew Camilleri 'Kukks' Feb 01 '15 at 17:24