I have a type, t
, and I would like to get a list of the public properties that have the attribute MyAttribute
. The attribute is marked with AllowMultiple = false
, like this:
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
Currently what I have is this, but I'm thinking there is a better way:
foreach (PropertyInfo prop in t.GetProperties())
{
object[] attributes = prop.GetCustomAttributes(typeof(MyAttribute), true);
if (attributes.Length == 1)
{
//Property with my custom attribute
}
}
How can I improve this? My apologies if this is a duplicate, there are a ton of reflection threads out there...seems like it's quite a hot topic.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…