简体   繁体   中英

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.

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 eg 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM