Composite attribute

Is there a way to make a composite attribute in C # to provide equivalent metadata at compile time?

For example, change:

[ClassInterface(ClassInterfaceType.AutoDual)] [ProgId("MyProgId")] [MyMefExport("MyProgId")] public class MyClass { } 

To:

 [MyCompositeAttribute("MyProgId")] public class MyClass { } 
+4
source share
2 answers

Attributes alone do not make sense. This is code that lists attributes attached to a class that can change its operation.

If you manage code that understands this attribute, you can create a composite attribute and act as if several separate attributes were applied.

Having said that, I doubt that you control ProgId and the C # compiler that interprets it ...

+2
source

Say you have the following code to read your custom attribute:

 var attrs = typeof(MyClass).GetCustomAttributes(typeof(MyCompositeAttribute) ,false); 

Any code that relies on says:

 var attrs = typeof(MyClass).GetCustomAttributes(typeof(ProgId) ,false); 

will not be able to return this attribute.

+1
source

Source: https://habr.com/ru/post/1447537/


All Articles