lundi 29 août 2016

c# get property with specific attribute from list objects

Having List of objects:

List<ConfigurationObjectBase> ObjectRegistry;

Having below Attribute and some of the above objects' decorated with that Attribute:

[AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
public sealed class PropertyCanHaveReference : Attribute
{
    public PropertyCanHaveReference(Type valueType)
    {
        this.ValueType = valueType;
    }

    public Type ValueType { get; set; }
}

Now, I want to find all objects whose property is decorated with that Attribute.

Trying below code, seems I am doing wrong:

List<ConfigurationObjectBase> tmplist = ObjectRegistry.Where(o => (o.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(PropertyCanHaveReference), true).Length > 0)));

Thanks for your time.





Aucun commentaire:

Enregistrer un commentaire