Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6188371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:13:12+00:00 2026-05-24T02:13:12+00:00

I have a custom attribute that I apply to properties on a class. This

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:13:13+00:00Added an answer on May 24, 2026 at 2:13 am
    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:

    1. Get the properties
    2. Throw them into an anonymous type so you have easy access to both the property and the attribute.
    3. Order by the FieldOrder, using -1 for properties without the attribute. (not sure what you wanted here)
    4. Transform the sequence back into a sequence of PropertyInfo
    5. Convert it into a PropertyInfo[] array.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which is marked with a custom attribute, like this: public
I have created a custom attribute that I am using on my class MyClass
I have a custom attribute which is applied to class properties and the class
I have a custom attribute that I want to apply to each methods in
I have a custom attribute which can be assigned to a class, [FooAttribute] .
I have a custom attribute and I need to have the name of the
I have a source object that looks like this: private class SourceObject { public
I have custom attribute defined like so: [AttributeUsage(AttributeTargets.Field)] public class EnumDisplayAttribute : Attribute {
I have a custom filter attribute that I want to use on certain ActionResults
Scenario: I have a base class MyBase. I have a custom attribute MyAttrib With

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.