elviuz
elviuz

Reputation: 639

Get all properties marked with [JsonIgnore] attribute

I have a class MyClass with a list of properties.

public class MyClass
{
    [Attribute1]
    [Attribute2]
    [JsonIgnore]
    public int? Prop1 { get; set; }

    [Attribute1]
    [Attribute8]
    public int? Prop2 { get; set; }

    [JsonIgnore]
    [Attribute2]
    public int Prop3 { get; set; }
}

I would like retrieve properties that are no marked with [JsonIgnore] attribute.

JsonIgnore is an attribute by http://www.newtonsoft.com/json

So, in this example, I would like to have the property "Prop2".

I tried with

var props = t.GetProperties().Where(
                prop => Attribute.IsDefined(prop, typeof(JsonIgnore)));

or

var props = t.GetProperties().Where(
                    prop => Attribute.IsDefined(prop, typeof(Newtonsoft.Json.JsonIgnoreAttribute)));

where t is the type of MyClass but the method return 0 elements.

can you help me, please? Thanks

Upvotes: 2

Views: 2446

Answers (3)

alwaysVBNET
alwaysVBNET

Reputation: 3310

This selects all the Names of the attributes with your IgnoreColumnAttribute attribute as an Array. Use !Attribute.IsDefined to select the opposite. This uses much less resources than the other answers due to the Attribute.IsDefined filtering vs the expensive GetCustomAttributes

dynamic props = typeof(MyClass).GetProperties().Where(prop =>
Attribute.IsDefined(prop, typeof(IgnoreColumnAttribute))).Select(propWithIgnoreColumn => 
propWithIgnoreColumn.Name).ToArray;

Upvotes: 0

Stefano d'Antonio
Stefano d'Antonio

Reputation: 6152

typeof(MyClass).GetProperties()
               .Where(property => 
                      property.GetCustomAttributes(false)
                              .OfType<JsonIgnoreAttribute>()
                              .Any()
                     );

Specifying the type in the GetCustomAttibutes call can be more performant, in addition, you might want the logic to be reusable so you could use this helper method:

static IEnumerable<PropertyInfo> GetPropertiesWithAttribute<TType, TAttribute>()
{
    Func<PropertyInfo, bool> matching = 
            property => property.GetCustomAttributes(typeof(TAttribute), false)
                                .Any();

    return typeof(TType).GetProperties().Where(matching);
}

Usage:

var properties = GetPropertyWithAttribute<MyClass, JsonIgnoreAttribute>();

EDIT: I'm not sure, but you might be after the properties without the attribute, so you could just negate the find predicate:

static IEnumerable<PropertyInfo> GetPropertiesWithoutAttribute<TType, TAttribute>()
{
    Func<PropertyInfo, bool> matching =
            property => !property.GetCustomAttributes(typeof(TAttribute), false)
                                .Any();

    return typeof(TType).GetProperties().Where(matching);
}

Or you could use simple libraries such as Fasterflect:

typeof(MyClass).PropertiesWith<JsonIgnoreAttribute>();

Upvotes: 9

Amit Kumar Ghosh
Amit Kumar Ghosh

Reputation: 3726

The attributes are on properties, not the class itself. So , you'll need to iterate over the properties, and then try to find those attributes -

foreach (var item in typeof(MyClass).GetProperties())
        {
            var attr = item.GetCustomAttributes(typeof(JsonIgnoreAttribute), false);
        }

Upvotes: 0

Related Questions