views:

35

answers:

1

ATM i cant quiet imagine how this will work. I'm sure it can be done. I notice a pattern use in my attribute where i always use 3 specific attributes together. Take the below as an example

    [MyAttr(4, @"a"),
     MyAttr(41, "b"),
     MyAttr(45, "ab")]

Mine is much more complicated but i would like to define one attribute with more params to generate the data above. How might i do that? Lets say my one attribute will look like this

MyAttr2(4, 41, "a", "b"); //4+41=45, "a"+"b" = "ab"

How might i generate the 3 MyAttr to apply to a class using MyAttr2?

+1  A: 

The C# compiler can't convert a single attribute entry into multiple ones in the assembly metadata. However, you could model your attribute in such as way that it exposes additional attribute information as properties (or a collection). However, the child information will not be accessible directly via reflection as independent attributes.

LBushkin
Hmm, so do i need to modify my code logic for MyAttr? Or can i leave it alone and do what your suggesting with the limitation of child information not being accessible directly?
acidzombie24
If you make the details part of a single attribute exposed via some collections/properties then you would need to change existing code. Which design better fits your needs is hard to say without more details about what you're doing and in how many cases you could consolidate multiple attributes. However, the child attribute approach would be a breaking change.
LBushkin