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 6318247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:37:06+00:00 2026-05-24T15:37:06+00:00

I have an interface that defines a method for returning an IList<PropertyInfo> : public

  • 0

I have an interface that defines a method for returning an IList<PropertyInfo> :

public interface IWriteable
{
    IList<PropertyInfo> WriteableProperties();
}

.
.
It is implemented in various (dissimilar) classes in the following manner:

public abstract class Foo
{
    private IList<PropertyInfo> _props;

    protected Foo()
    {
        this._props = new List<PropertyInfo>();

        foreach (PropertyInfo p in this.GetType().GetProperties())
        {
            if (Attribute.IsDefined(p, typeof(WriteableAttribute)))
                this._props.Add(p);
        }
    }

    #region IWriteable Members

    public IList<PropertyInfo> WriteableProperties()
    {
        return this._props;
    }

    #endregion
}

public class Bar : Foo
{
    public string A
    {
        get { return "A"; }
    }

    [Writeable()]
    public string B
    {
        get { return "B"; }
    }

    [Writeable()]
    public string C
    {
        get { return "C"; }
    }

    // Snip
}

Please note the attributes marking a couple of the properties, as these are the properties that will get added to the list. This IList will then be used elsewhere during some file write operations.

It is important to me that they are ordered in the list in the order they appear in the code file.

However, MSDN states:

The GetProperties method does not return properties in a particular
order, such as alphabetical or declaration order. Your code must not
depend on the order in which properties are returned, because that
order varies.

So, what is the best way to ensure each PropertyInfo gets added in the order I would like to to be?

(I am also using .NET2.0, so I can’t use any Linq goodness, should there be any that would help, although it would be interesting to see.)

  • 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-24T15:37:06+00:00Added an answer on May 24, 2026 at 3:37 pm

    Add information to the attribute about the ordering, you can then use this to ensure the ordering, e.g.:

    [Writeable(Order = 1)]
    

    So for the following attribute:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class WriteableAttribute : Attribute
    {
        public int Order { get; set; }
    }
    

    You can get an ordered selection of the properties as follows:

    private readonly List<PropertyInfo> _props;
    
    protected Foo()
    {
        _props = new List<PropertyInfo>();
    
        var props = new Dictionary<int, PropertyInfo>();
    
        foreach (PropertyInfo p in GetType().GetProperties())
        {
            if (Attribute.IsDefined(p, typeof(WriteableAttribute)))
            {
                var attr = (WriteableAttribute)p
                    .GetCustomAttributes(typeof(WriteableAttribute))[0];
    
                props.Add(attr.Order, p);
            }
        }
    
        _props.AddRange(props.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value));
    }
    

    NB For production code I would recommend caching the property information (per type for example) as this will be relatively slow if carried out for each instance.

    Update – Caching

    With some example caching of property lookup and ordering:

    public static class PropertyReflector
    {
        private static readonly object SyncObj = new object();
    
        private static readonly Dictionary<Type, List<PropertyInfo>> PropLookup =
            new Dictionary<Type, List<PropertyInfo>>();
    
        public static IList<PropertyInfo> GetWritableProperties(Type type)
        {
            lock (SyncObj)
            {
                List<PropertyInfo> props;
    
                if (!PropLookup.TryGetValue(type, out props))
                {
                    var propsOrder = new Dictionary<int, PropertyInfo>();
    
                    foreach (PropertyInfo p in type.GetProperties())
                    {
                        if (Attribute.IsDefined(p, typeof(WriteableAttribute)))
                        {
                            var attr = (WriteableAttribute)p.GetCustomAttributes(
                                typeof(WriteableAttribute), inherit: true)[0];
    
                            propsOrder.Add(attr.Order, p);
                        }
                    }
    
                    props = new List<PropertyInfo>(propsOrder
                        .OrderBy(kvp => kvp.Key)
                        .Select(kvp => kvp.Value));
    
                    PropLookup.Add(type, props);
                }
    
                return props;
            }
        }
    }
    

    Update – No Linq

    You can replace the Linq section with the following code to order the properties and add them to the cache:

    List<int> order = new List<int>(propsOrder.Keys);
    order.Sort();
    
    props = new List<PropertyInfo>();
    
    order.ForEach(i => props.Add(propsOrder[i]));
    
    PropLookup.Add(type, props);
    

    Update – Full Linq

    And using a fully Linq solution:

    static IList<PropertyInfo> GetWritableProperties(Type type)
    {
        lock (SyncObj)
        {
            List<PropertyInfo> props;
    
            if (!PropLookup.TryGetValue(type, out props))
            {
                props = type.GetProperties()
                    .Select(p => new { p, Atts = p.GetCustomAttributes(typeof(WriteableAttribute), inherit: true) })
                    .Where(p => p.Atts.Length != 0)
                    .OrderBy(p => ((WriteableAttribute)p.Atts[0]).Order)
                    .Select(p => p.p)
                    .ToList();
    
                PropLookup.Add(type, props);
            }
    
            return props;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface that defines some methods I would like certain classes to
I have a interface that defines some methods with attributes. These attributes need to
I have a very thin interface that's going to define one method. Should I
Let's say I have an interface IAutoTask and few other classes implementing that interface,
I have a WCF service which has one method returning a stream. [ServiceContract] public
I have an interface called iIncident which defines a single method when() . when()
I have an interface that I have defined in C++ which now needs to
I have an NSWindow that I defined in interface builder. I want to make
I have an interface that needs to react to long key presses. That means
Lets say you have interface definition. That interface can be Operation . Then you

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.