How to get a list of properties with a given attribute?

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.

Get All properties that has a custom attribute with specific values [duplicate]

Possible Duplicate: How to get a list of properties with a given attribute? I have a custom class like this public class ClassWithCustomAttributecs { [UseInReporte(Use=true)] public int F1 { get; se

How to get the list of properties of a class?

How do I get a list of all the properties of a class?

How to get list of attribute names from attribute set

I want to get a list of names of attributes from attribute set. I know how to get attribute IDs, but I can’t figure out how to get list of names. This is my code so far. <?php $entityTypeId = Mage:

Accessing properties attribute inside the get accessor

Am creating a custom attribute for my properties and was wondering if anyone know how i could access the the value of the Attribute inside of the get accessor. public class MyClass { [Guid({2017ECDA-

Get properties with attribute

I am trying to solve a problem how to get a required properties from the class. I can use function like GetPropertiesForSerialization, but I would like to use Attributes. My idea is that some class pr

Get all attribute properties [duplicate]

This question already has an answer here: Get all Attributes from a HTML element with Javascript/jQuery 6 answers I would like to get all the attribute properties with a Jquery event. I know I

How to get css properties set explicitly via the style attribute? [closed]

How do I find all the css properties set explicitly via the style attribute?

Get a list of observable properties

i know how to use the command ‘properties’ to get a list of all properties that an object has. Is there a way to get a list of only the properties that are observable?

Get all properties which marked certain attribute

I have class and properties in there. Some properties can be marked attribute (it’s my LocalizedDisplayName inherits from DisplayNameAttribute). This is method for get all properties of class: private

List object properties that have an attribute applied

I have a custom attribute applied to some properties in a class. What is the most efficient way to retrieve a list of the properties that have the attribute applied? Do you have to reflect on the list


the better way:

if (attributes.Length == 1)
// |
// |
// |
// .
if (attributes.Length != 0)

As far as I know, there isn’t any better way in terms of working with Reflection library in a smarter way. However, you could use LINQ to make the code a bit nicer:

var props = from p in t.GetProperties()
            let attrs = prop.GetCustomAttributes(typeof(MyAttribute), true)
            where attrs.Length != 0 select p;

// Do something with the properties in 'props'

I believe this helps you to structure the code in a more readable fashion.

There’s always LINQ:

    p=>p.GetCustomAttributes(typeof(MyAttribute), true).Length != 0)

If you deal regularly with Attributes in Reflection, it is very, very practical to define some extension methods. You will see that in many projects out there. This one here is one I often have:

public static bool HasAttribute<T>(this ICustomAttributeProvider provider) where T : Attribute
  var atts = provider.GetCustomAttributes(typeof(T), true);
  return atts.Length > 0;

which you can use like typeof(Foo).HasAttribute<BarAttribute>();

Other projects (e.g. StructureMap) have full-fledged ReflectionHelper classes that use Expression trees to have a fine syntax to identity e.g. PropertyInfos. Usage then looks like that:

ReflectionHelper.GetProperty<Foo>(x => x.MyProperty).HasAttribute<BarAttribute>()
var props = t.GetProperties().Where(
                prop => Attribute.IsDefined(prop, typeof(MyAttribute)));

This avoids having to materialize any attribute instances (i.e. it is cheaper than GetCustomAttribute[s]().

The solution I end up using most is based off of Tomas Petricek’s answer. I usually want to do something with both the attribute and property.

var props = from p in this.GetType().GetProperties()
            let attr = p.GetCustomAttributes(typeof(MyAttribute), true)
            where attr.Length == 1
            select new { Property = p, Attribute = attr.First() as MyAttribute};