0

As stated in the title I am asking if it's possible to define an attribute as a combination of an existing one. When I talk about an attribute I mean a class that inherits from System.Attribute.

I ask that because I found that a lot of my code looks like this :

[LibraryA_Attribute(string_param)]
[LibraryB_Attribute(int_param)]
public int MyProperty { get; set; }

I would like to not need to remember the list of attributes that I need to use every time, to decorate my POCO class, in order to get them managed correctly by the library I use. I would like to write something like this :

[MyAttribute(string_param,int_param)]
public int MyProperty { get; set; }

and let the compiler translate it properly.

My first thought is to implicit cast to convert MyAttribute to LibraryA_Attribute or LibraryB_Attribute but I'm not sure if it will work (plus it sounds risky). So I am asking for a suggestion.

seandon47
  • 286
  • 1
  • 6
Skary
  • 1,322
  • 1
  • 13
  • 40
  • Might help: https://stackoverflow.com/questions/60781467/add-attributes-inside-custom-attribute-in-c-sharp – Christoph Lütjen Jan 15 '21 at 17:41
  • @ChristophLütjen thanks for suggestion, but i think that emit a new type with custom attribute will made my POCO calss less usable (i should hide constructor or add a convert method) and problematic. Not to said that the libraries i use will serialize that POCO class and that libraries need to know the right type to use. – Skary Jan 15 '21 at 17:48

0 Answers0