I have a custom attribute that I apply to properties on a class. This attribute is used for exporting the class's properties to a flat file.
One of the attribute's properties is FieldOrder.  I need to make sure the order in which I export the properties of the class is correct.  Also, not all properties on the class will have the custom attribute.
I found this article: How do I sort a generic list based on a custom attribute? This solution assumes all properties have the custom attribute, which isn't my case. I was also hoping for more elegant solution.
Your help is greatly appreciated!
public interface IFileExport{}
public class ExportAttribute: Attribute
{
    public int FieldOrder { get; set; }
    public int FieldLength { get; set; }
    public ExportAttribute() { }
}
public class ExportClass: IFileExport
{
    [ExportAttribute( FieldOrder = 2, FieldLength = 25 )]
    public string LastName { get; set; }
    [ExportAttribute( FieldOrder=1, FieldLength=25)]
    public string FirstName { get; set; }
    [ExportAttribute( FieldOrder = 3, FieldLength = 3 )]
    public int Age { get; set; }
    public ExportClass() { }
}
public class TestClass
{
    public static List<PropertyInfo> GetPropertiesSortedByFieldOrder
                                                            (IFileExport fileExport)
    {
        //get all properties on the IFileExport object
        PropertyInfo[] allProperties = fileExport
                         .GetType()
                         .GetProperties( BindingFlags.Instance | BindingFlags.Public );
        // now I need to figure out which properties have the ExportAttribute 
        //and sort them by the ExportAttribute.FieldOrder
    }
}
UPDATE: I'm ordering the properties by ExportAttribute.FieldOrder Ascending
public static List<PropertyInfo> GetPropertiesSortedByFieldOrder( IFileExport    fileExport )
{
    PropertyInfo[] allProperties = GetType()
        .GetProperties(BindingFlags.Instance | BindingFlags.Public)
        .Select(x => new 
        { 
            Property = x, 
            Attribute = (ExportAttribute)Attribute.GetCustomAttribute(x, typeof(ExportAttribute), true) 
        })
        .OrderBy(x => x.Attribute != null ? x.Attribute.FieldOrder : -1)
        .Select(x => x.Property)
        .ToArray();
}
Since you didn't explain how you wanted properties without the attribute ordered, I have made it so that they would be at the beginning. But the gist of this code is:
FieldOrder, using -1 for properties without the attribute.  (not sure what you wanted here)PropertyInfo
PropertyInfo[] array. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With