lundi 29 août 2016

c# - 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://ift.tt/1JSZVlU

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





Aucun commentaire:

Enregistrer un commentaire