开发者

C# Get FieldInfos/PropertyInfos in the original order?

How can I get a Types FieldInfos/PropertyInfos as a MemberInfo array in the order they are laid out in the class?

class Test
{
    public bool First { get; set; }
    public int Second开发者_如何学运维;
    public string Third { get; set; }
}


http://msdn.microsoft.com/en-us/library/ch9714z3.aspx

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

http://msdn.microsoft.com/en-us/library/kyaxdd3x.aspx

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.

You would need to define order yourself, perhaps with attributes:

class Test
{
    [Order(1)] public bool First { get; set; }
    [Order(2)] public int Second;
    [Order(3)] public string Third { get; set; }
}
...
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, 
    Inherited = true, AllowMultiple = false)]
[ImmutableObject(true)]
public sealed class OrderAttribute : Attribute {
    private readonly int order;
    public int Order { get { return order; } }
    public OrderAttribute(int order) {this.order = order;}
}


Look at Mono.Cecil

If the Serializer is able to do source ordering it won't be because of the PDB debug info.

I assume reflection loses the ordering because it will (potentially) return a mix of direct and inherited members. There is no 'correct' ordering of that mix.

Mono.Cecil will let you get directly at the structures in the managed assembly, as well as at the CIL code. Mono.cecil rocks big time and will not eat your puppies. It is the fastestest method to analyze your assemblies, and you don't even have to have them loaded.

Mono.Cecil goes on to write a new assembly if you wish, but this propaganda is getting way off-topic.

Go get Mono.Cecil


You can't, as this info is not relevant to the execution or functionality of the class. Why would you want to retrieve this info anyway?


The line number information is not compiled in to the assembly, it is stored in the .PDB file for the use of debugger.
Although technically it may be possible to get the information you are looking from the PDB file, I don't think that will be a good idea either, as the PDB file will not be present in the production environment. Even if it is there there is no guarantee that it is in sync with the DLL.


I have found more information when trying to google the other way around. Like JbEvain pointed out, it is confirmed that there is no way to control the order in which the compiler outputs members in CIL classes. This even pertains to XmlSerializer

A number of interesting posts have posted here:

On that topic, I found that in fact to have reliable ordering (In .NET 2.0, you can also “explicitly“ control this using the XmlElementAttribute.Order)[pluralsight-training.net/community/blogs/tewald/archive/2006/04/… and others

  • http://www.pluralsight-training.net/community/blogs/craig/archive/2006/04/06/21176.aspx
  • http://www.pluralsight-training.net/community/blogs/craig/archive/2006/04/18/21933.aspx
  • http://www.pluralsight-training.net/community/blogs/tewald/archive/2006/04/18/21964.aspx

This should give a good background to this discussion. It now really depends on what the original poster needed this information for whether there can even be a solution, and if so, to find a route to achieving that goal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜