While it is possible for you to specify the same Attribute
class multiple times, it may be more beneficial to define each property as a separate, independent attribute.
For example VisibilityAttribute
, MyAttributeAttribute
, ... etc. You could then apply each attribute individually to properties.
If it seems cumbersome to reflect each manually, you could make a helper method that would reflect all the possible ones and return back a Dictionary
object with each one's key/value.
For example:
[Visibility("true")]
[MyAttribute("Something")]
Then you could have a helper method that does something like this:
static Dictionary<string, string> GetAttributeDictionary(object value)
{
var dictionary = new Dictionary<string, string>();
var type = value.GetType();
var customAttributes = type.GetCustomAttributes(true);
foreach (var attribute in customAttributes)
{
if (attribute is VisibilityAttribute)
{
var visibilityAttribute = attribute as VisibilityAttribute;
dictionary["Visibility"] = visibilityAttribute.Visibility;
}
// Process other custom attributes...
}
return dictionary;
}