title: How can I get MemberInfos of a class in the order the members are declared?
body: <p>Let's say I have the following class:</p>
  
  <pre><code>public class Target
  {
     public int field1;
     public int field2;
     public int Prop1 { get; set; }
     public int Prop2 { get; set; }
  }
  </code></pre>
  
  <p>If I do a:</p>
  
  <pre><code>foreach(far f in typeof(Target).GetFields())
     Console.WriteLine(f.Name);
  </code></pre>
  
  <p>I would get the fields in the order they're declared in (<code>field1</code> then <code>field2</code>). Same thing if I did <code>GetProperties</code></p>
  
  <p>Now, consider the following:</p>
  
  <pre><code>public class Target
  {
     public int field1;
     public int Prop1 { get; set; }
     public int field2;
     public int Prop2 { get; set; }
  }
  </code></pre>
  
  <p>When I did:</p>
  
  <pre><code>    var members = typeof(Target).GetMembers()
                   .Where(m =&gt; m.MemberType == MemberTypes.Field ||
                               m.MemberType == MemberTypes.Property);
  
      foreach (var m in members)
          Console.WriteLine(m.Name);
  </code></pre>
  
  <p>I got the following output:</p>
  
  <pre><code>Prop1
  Prop2
  field1
  field2
  </code></pre>
  
  <p>Obviously, not in the order the members are declared in (which is <code>field1</code>, <code>Prop1</code>, <code>field2</code> and <code>Prop2</code>)</p>
  
  <p>Is there any way I can get the members in that order? </p>
  
  <p>Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>I care about the order, because I work with Unity3D (Game engine) and I'm writing a custom editor for a certain type. The type will have fields and properties, I want to draw the fields/properties in the order that they're declared in. Of course, unity does draw the fields in the order they're declared in, but it doesn't draw properties. That's why I'm making my own drawers/editors to support property drawing.</p>
  
  <p><img src="https://i.stack.imgur.com/mzHXi.png" alt="enter image description here"></p>