MRebati
MRebati

Reputation: 599

Attributes that implement other attributes

I want to facade or proxy a library's implemented attribute. for example the property mapping attributes.

I want to control which of the libraries attributes to be used in project. some kind of a DI or Module control. Is there an easy way to achieve it without using reflection?

something to transfer these two types of attributes into one:

[MapsFromAndToProperty(typeof(fooClass), nameof(fooClass.PropertyName))]

and

[AdaptMember(name)]

into

[MyAttributeToControlWhichOneToUse(typeof(fooClass), nameof(fooClass.PropertyName))]

And Thank you for your attention. Any help would be appreciated.

Upvotes: 0

Views: 171

Answers (1)

Georg
Georg

Reputation: 5791

As noted in the comments, it is not possible to alter attributes in the general case. This is simply because attributes are effectively part of the assembly metadata and doing what you want means to change that assembly metadata.

There are two ways how this could potentially work, though:

  1. You do change the assembly metadata. Here, you are in the realm of aspect-oriented programming. You could try to create an aspect e.g. based on PostSharp that unrolls your MyAttributeToControlWhichOneToUse attribute into the required attributes at compile-time. This solution is independent of the way how the attributs are read, but requires changes to the build infrastructure.
  2. You alter the way how the attributes are loaded. MEF is an example that does that for its convention-based programming model that effectively emulates attributes on the conventional classes by altering the way how attributes are retrieved. This only works, if foreseen in the lib that reads the properties (and honestly, I have never seen anybody else than MEF to do that).

Upvotes: 1

Related Questions