C # Get FieldInfos / PropertyInfos in Original Order?

How can I get the FieldInfos / PropertyInfos types as a MemberInfo array in the order they are placed in the class?

class Test { public bool First { get; set; } public int Second; public string Third { get; set; } } 
+20
c #
Mar 29 '11 at 13:38
source share
5 answers

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

The GetFields method does not return fields in a specific order, for example, in alphabetical order or in declaration order. Your code should not depend on the order in which the fields are returned, as this order changes.

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

The GetProperties method does not return properties in a specific order, for example, in alphabetical order or in declaration order. Your code should not depend on the order of returning the properties, since this order is changing.

You will need to determine the order yourself, possibly 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;} } 
+23
Mar 29 '11 at 13:44
source share

You cannot, as this information does not apply to the execution or functionality of the class. Why do you still want to receive this information?

+2
Mar 29 2018-11-23T00:
source share

Look at Mono.Cecil

If the Serializer can perform source code sequencing, this will not be due to PDB debugging information.

I assume that reflection is losing order because it (potentially) returns a combination of direct and inherited members. There is no “right” order of this combination.

Mono.Cecil allows you to directly access managed assembly structures as well as CIL code. Mono.cecil bites a lot of time and will not eat your puppies. This is the fastest way to analyze your assemblies, and you don’t even need to download them.

Mono.Cecil continues to write a new build if you want, but this propaganda leaves the topic.

Go get Mono.Cecil

+2
Mar 29 '11 at 15:06
source share

Information about the line number is not compiled into the assembly; it is stored in the .PDB file for use by the debugger.
Although it is technically possible to get the information you are looking for from a PDB file, I don’t think it would be a good idea since the PDB file will not be present in the production environment. Even if it is, there is no guarantee that it synchronizes with the DLL.

+1
Mar 29 '11 at 1:47 a.m.
source share

I found more information when trying Google differently. As JbEvain noted, it is confirmed that there is no way to control the order in which the compiler displays members in CIL classes. This one even applies to the XmlSerializer

Several interesting posts are posted here:

In this thread, I found that it actually has a reliable order (in .NET 2.0, you can also “explicitly” control this using XmlElementAttribute.Order) [multipalsight-training.net/community/blogs/tewald/archive/2006/04 /... and others

This should serve as a good basis for discussion. Now it really depends on the fact that the original poster needed this information in order to even have a solution, and if so, find a way to achieve this goal.

+1
Mar 29 '11 at 18:54
source share



All Articles