English 中文(简体)
“Type.GetProperties' Property Order
原标题:`Type.GetProperties` property order

http://www.ohchr.org。

Type.GetProperties<>指出,其收复保证不按字母或申报顺序进行,尽管经过简单的测试显示,一般来说,其返还是按申报顺序进行的。 是否有具体假设,你知道情况并非如此? 除此之外,所建议的选择是什么?

详细版

我实现了Type.GetProperties。 国家:

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 there is no guarantee that the collection returned by the method will be ordered any specific way. Based on some tests, I ve found to the contrary that the properties returned appear in the order they re defined in the type.

Example:

class Simple
{
    public int FieldB { get; set; }
    public string FieldA { get; set; }
    public byte FieldC { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Simple Properties:");
        foreach (var propInfo in typeof(Simple).GetProperties())
            Console.WriteLine("	{0}", propInfo.Name);
    }
}

产出:

Simple Properties:
        FieldB
        FieldA
        FieldC

一种情况是,该类型有父母也有财产:

class Parent
{
    public int ParentFieldB { get; set; }
    public string ParentFieldA { get; set; }
    public byte ParentFieldC { get; set; }
}

class Child : Parent
{
    public int ChildFieldB { get; set; }
    public string ChildFieldA { get; set; }
    public byte ChildFieldC { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Parent Properties:");
        foreach (var propInfo in typeof(Parent).GetProperties())
            Console.WriteLine("	{0}", propInfo.Name);

        Console.WriteLine("Child Properties:");
        foreach (var propInfo in typeof(Child).GetProperties())
            Console.WriteLine("	{0}", propInfo.Name);

    }
}

产出:

Parent Properties:
        ParentFieldB
        ParentFieldA
        ParentFieldC
Child Properties:
        ChildFieldB
        ChildFieldA
        ChildFieldC
        ParentFieldB
        ParentFieldA
        ParentFieldC

Which means the GetProperties method walks up the inheritance chain from bottom up when discovering the properties. That s fine and can be handled as such.

问题:

  1. Are there specific situations where the described behavior would differ that I ve missed?
  2. If depending on the order is not recommended then what is the recommended approach?

一种似乎显而易见的解决办法是界定一种习俗特性,表明其特性(should)的顺序(与>Order上的不动产(的属性)。 类似:

public class PropOrderAttribute : Attribute
{
    public int SeqNbr { get; set; }
}

其后实施:

class Simple
{
    [PropOrder(SeqNbr = 0)]
    public int FieldB { get; set; }
    [PropOrder(SeqNbr = 1)]
    public string FieldA { get; set; }
    [PropOrder(SeqNbr = 2)]
    public byte FieldC { get; set; }
}

But as many have found, this becomes a serious maintenance problem if your type has 100 properties and you need to add one between the first 2.

UPDATE

这里列举的例子只是为了示范目的。 在我的具体设想中,我用一个类别界定了信息格式,然后通过该类的特性加以篡改,并 gr清其属性,以了解如何对电文中的具体领域进行划界。 电文中的田地顺序意义重大,因此,本类房地产的顺序必须相当高。

It works currently by just iterating over the return collection from GetProperties, but since the documentation states it is not recommended I was looking to understand why and what other option do I have?

最佳回答
问题回答

我使用习俗属性来补充必要的元数据(它用的是像消费和回报CRLF的有限钥匙=Value的同类服务)。

第一,习俗归属:

class ParameterOrderAttribute : Attribute
{
    public int Order { get; private set; }
    public ParameterOrderAttribute(int order)
    {
        Order = order;
    }
}

然后,贬低你的班子:

class Response : Message
{
    [ParameterOrder(0)]
    public int Code { get; set; }
}

class RegionsResponse : Response 
{
    [ParameterOrder(1)]
    public string Regions { get; set; }
}

class HousesResponse : Response
{
    public string Houses { get; set; }
}

一种手法,将财产信息转换成一种可分类的:

    private int PropertyOrder(PropertyInfo propInfo)
    {
        int output;
        var orderAttr = (ParameterOrderAttribute)propInfo.GetCustomAttributes(typeof(ParameterOrderAttribute), true).SingleOrDefault();
        output = orderAttr != null ? orderAttr.Order : Int32.MaxValue;
        return output;
    }

更糟的是,文字是延伸:

static class PropertyInfoExtensions
{
    private static int PropertyOrder(this PropertyInfo propInfo)
    {
        int output;
        var orderAttr = (ParameterOrderAttribute)propInfo.GetCustomAttributes(typeof(ParameterOrderAttribute), true).SingleOrDefault();
        output = orderAttr != null ? orderAttr.Order : Int32.MaxValue;
        return output;
    }
}

最后,你现在可以问你。 物体类型:

        var props = from p in type.GetProperties()
                    where p.CanWrite
                    orderby p.PropertyOrder() ascending
                    select p;

依靠明确记载为、未保证<>/em>的执行细节是灾害的对应办法。

建议的做法会因你一旦拥有这些财产而想做些什么而有所不同。 是否在屏幕上展示? 按成员类别(财产、外地、职能)分列的MSDN docs组,然后按组别内的字母顺序排列。

如果你的信息格式取决于实地的次序,那么你要么需要:

  1. 在某种信息定义中具体说明预期顺序。 http://code.google.com/p/protobuf/“rel=“nofollow” 如果我回顾一下,电文定义就是在该案中汇编的,从一个文件上编成法典文件,以你当时正在使用的任何语言使用。

  2. Rely on an order that can be independently generated, e.g. alphabetical order.

<><>>>>>

I ve spent the last day troubleshooting a problem in an MVC 3 project, and it all came down to this particular problem. It basically relied on the property order being the same throughout the session, but on some occations a few of the properties switched places, messing up the site.

首先,在动态分类表中界定列名的编号为<代码>Type.GetProperties()的代码。 其后的<代码>Type.GetProperties ()方法被称作是填表上的实际数据,有时,这些财产被转用,并且完全上报。 在某些情况下,该站点依赖的等级子网的其他特性被转用,即,由于ID栏含有错误数据,你不能再看到次数据。 换言之:yes,这肯定会发生。 阅读。

<><>>>>

If you need consistent order throughout the system session but not nessecarily exactly the same order for all sessions the workaround is dead simple: store the PropertyInfo[] array you get from Type.GetProperties() as a value in the webcache or in a dictionary with the type (or typename) as the cache/dictionary key. Subsequently, whenever you re about to do a Type.GetProperties(), instead substitute it for HttpRuntime.Cache.Get(Type/Typename) or Dictionary.TryGetValue(Type/Typename, out PropertyInfo[]). In this way you ll be guaranteed to always get the order you encountered the first time.

If you always need the same order (i.e. for all system sessions) I suggest you combine the above approach with some type of configuration mechanism, i.e. specify the order in the web.config/app.config, sort the PropertyInfo[] array you get from Type.GetProperties() according to the specified order, and then store it in cache/static dictionary.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...