mardi 8 mars 2016

C# recrusive limit all properties of type List with help of an attribute

I have a very complex class, in which same properties of type List should be limited and the last items should be marked with a message. So i created I Attribute, called RangeLimitAttribute, which contains the limit, the message and the property for the message.

What is a good was to recursive get all affected properties? And set the Message to the last item?

public class RangeLimitAttribute : Attribute
{
    public RangeLimitAttribute(int maxCount, string message, string messagePropertyname)
    {
        MaxCount = maxCount;
        this.Message = message;
        this.MessagePropertyname = messagePropertyname;
    }

    public string MessagePropertyname { get; set; }

    public string Message { get; set; }

    public int MaxCount { get; set; }
}

Sample class:

public class Class
{
    [XmlAttribute("DisplayNameXYZ")]
    public string DisplayName { get; set; }

    [XmlAttribute("ErrorsXYZ")]
    [RangeLimit(100, "... more Errors", "Message")]
    public List<NestedClass> Errors { get; set; }
}

public class NestedClass
{
    [XmlAttribute("MessageXYZ")]
    public string Message { get; set; }

    [XmlElement("FilesXYZ")]
    [RangeLimit(100, "... more Errors", "OtherProperty")]
    public List<File> Files { get; set; }
}





Aucun commentaire:

Enregistrer un commentaire