class MyAttribute:Attribute {
public MyAttribute( string someValue ) {
SomeValue = someValue;
}
publicString SomeValue { get; privateset; }
publicString SomeOtherValue { get; set; }
}
classStudent {
[My("Howdy", SomeOtherValue = "there!")]
publicString Nome { get; set; }
publicString Morada { get; set; }
}
In the previous post, we’ve already saw how to check if an attribute is applied to a specific target. So, we won’t be performing that check again and I’ll just show you how to recover the values applied to the properties of the attribute:
var prop = typeof( Student ).GetProperty( “Nome” );
var attrib = CustomAttributeData.GetCustomAttributes( prop ).First();
//positional arguments can be obtained from the ConstructorArguments prop
var positionalArgs = attrib.ConstructorArguments;
for(var i = 0; i < positionalArgs.Count; i++){
Console.WriteLine( “arg at pos {0} has value {1}“,
i, positionalArgs[i].Value);
}
Read more: LA.NET [EN]
QR: