lundi 15 août 2016

Using reflection to identify properties with a given decorator

I'm trying to get a list of properties in a class where the property is marked with the decorator [Ignore] (SQLite.Net), although I believe the question is applicable for any decorator.

var ignored = typeof(T)
            .GetType()
            .GetProperties().Where(p =>
                p.GetCustomAttributes<SQLite.Net.Attributes.IgnoreAttribute>())
            .ToList();

I've tried various combinations of this - the one here doesn't even compile, but accessing the p.CustomAttributes collection does; however, it doesn't return the correct property. For completeness, here is the property in T:

private ProductCategory _category;

[Ignore]
public ProductCategory Category
{
    get { return _category; }
    set
...

Please could someone point me in the right direction here - are CustomAttributes even the right place to be looking for this?





Aucun commentaire:

Enregistrer un commentaire