列表中属性的保证顺序与它们在代码文件中出现的顺序相匹配

时间:2011-08-16 19:56:40

标签: c# list reflection c#-2.0

我有一个界面,用于定义返回IList<PropertyInfo>的方法:

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



它以下列方式在各种(不同的)类中实现:

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
}

请注意标记几个属性的属性,因为这些属性将添加到列表中。在某些文件写入操作期间,此IList将在其他地方使用。

对我来说,按照它们在代码文件中出现的顺序在列表中排序是很重要的。

然而,MSDN声明:

  

GetProperties方法不返回特定属性   订单,例如字母或声明订单。你的代码一定不能   取决于返回属性的顺序,因为那样   订单各不相同。

那么,确保按照我希望的顺序添加每个PropertyInfo的最佳方法是什么?

(我也使用.NET2.0,所以我不能使用任何Linq的优点,如果有任何帮助,虽然看到它会很有趣。)

2 个答案:

答案 0 :(得分:6)

向属性添加有关排序的信息,然后您可以使用它来确保排序,例如:

[Writeable(Order = 1)]

所以对于以下属性:

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

您可以按如下方式获得有序选择的属性:

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 对于生产代码,我建议缓存属性信息(例如每种类型),因为如果为每个实例执行此操作将会相对较慢。

更新 - 缓存

通过一些示例缓存属性查找和排序:

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;
        }
    }
}

更新 - No Linq

您可以使用以下代码替换Linq部分,以对属性进行排序并将其添加到缓存中:

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);

更新 - Full Linq

使用完全Linq解决方案:

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;
    }
}

答案 1 :(得分:1)

不久之前,当我遇到同样的问题时,我编写了一个帮助器类,根据属性的Order属性对属性进行排序。我使用了内置的DisplayAttribute,但您只需向您编写的任何属性添加Order属性。

class FieldSorter : IComparer, IComparer<DisplayAttribute>, IEqualityComparer<DisplayAttribute>
{
    public int Compare(object x, object y)
    {
        return Compare((DisplayAttribute)x, (DisplayAttribute)y);
    }
    public int Compare(DisplayAttribute x, DisplayAttribute y)
    {
        return x.Order.CompareTo(y.Order);
    }
    public bool Equals(DisplayAttribute x, DisplayAttribute y)
    {
        return Compare(x, y) == 0;
    }
    public int GetHashCode(DisplayAttribute obj)
    {
        return obj.GetHashCode();
    }

    public static SortedList<DisplayAttribute, PropertyInfo> GetSortedFields(Type type)
    {
        PropertyInfo[] props = type.GetProperties();
        var sortedProps = new SortedList<DisplayAttribute, PropertyInfo>(props.Length, new FieldSorter());
        object[] atts;
        int assignedOrder = 1000; // anything without pre-assigned order gets a ridiculously high order value. same for duplicates.
        foreach (var prop in props)
        {
            atts = prop.GetCustomAttributes(typeof(DisplayAttribute), true);
            if (atts.Length > 0)
            {
                var att = (DisplayAttribute)atts[0];
                if (!att.GetOrder().HasValue || sortedProps.Keys.Contains(att, new FieldSorter()))
                    att.Order = assignedOrder++;
                sortedProps.Add(att, prop);
            }
        }
        return sortedProps;
    }
}

这会给你一个SortedList,其中键是属性,值是PropertyInfo。这是因为我仍然需要访问该属性的其他属性。

使用示例:

        public class Stats 
        {
            [Display(Name = "Changes", Description = "Changed records.", Order = 8)]
            public int RecordsWithChanges { get; set; }
            [Display(Name = "Invalid", Description = "Number of invalid records analyzed.", Order = 4)]
            public int InvalidRecordCount { get; set; }
            [Display(Name = "Valid", Description = "Number of valid records.", Order = 6)]
            public int ValidRecordCount { get; set; }
            [Display(Name = "Cost", Description = "Number of records with a Cost value.", Order = 10)]
            public int RecordsWithCost { get; set; }
            public Stats(int changed, int valid, int invalid, int cost)
            {
                RecordsWithChanges = changed;
                ValidRecordCount = valid;
                InvalidRecordCount = invalid;
                RecordsWithCost = cost;
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                var foo = new Stats(123, 456, 7, 89);
                var fields = FieldSorter.GetSortedFields(foo.GetType());
                foreach (DisplayAttribute att in fields.Keys)
                    Console.WriteLine("{0}: {1} ({2}) == {3}", 
                        att.Order, att.Name, att.Description, fields[att].GetValue(foo, null));
null));

            }
        }

输出:

4: Invalid (Number of invalid records analyzed.) -- 7
6: Valid (Number of valid records.) -- 456
8: Changes (Changed records.) -- 123
10: Cost (Number of records with a Cost value.) -- 89