Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, Tim S. gave you a pretty solid answer.</p> <p>However, if those <code>various variables</code> you mentioned are fields and not properties, it won't work. You could just change it to <code>GetFields</code> instead of <code>GetProperties</code> (and GetValue wouldn't need the second <code>null</code> parameter)</p> <p>Here's another example which would work with fields (using linq, a bit shorter):</p> <pre><code>class A { public string Field1; public string Field2; public string Field3; } static void Main(string[] args) { A myA = new A() { Field1 = "rofl", Field2 = "lol", Field3 = "omg" }; var obj = (from prop in typeof(A).GetFields(BindingFlags.Public | BindingFlags.Instance) select prop.GetValue(myA)).ToArray(); Debug.Assert(obj[0] == "rofl"); Debug.Assert(obj[1] == "lol"); Debug.Assert(obj[2] == "omg"); } </code></pre> <p>If you'd like it to work with properties, just replace the linq with this:</p> <pre><code>var obj2 = (from prop in typeof(A).GetProperties(BindingFlags.Public | BindingFlags.Instance) select prop.GetValue(myA,null)).ToArray(); </code></pre> <p>I typed <code>var</code> to be short but <code>obj</code> and <code>obj2</code> are of type <code>Object[]</code></p> <p><strong>EDIT:</strong></p> <p>As specified by <a href="http://msdn.microsoft.com/en-us/library/kyaxdd3x%28v=vs.110%29.aspx" rel="nofollow">MSDN</a>, </p> <blockquote> <p>The GetProperties method does not return properties in a particular order, such as alphabetical or declaration order.</p> </blockquote> <p>So if you want to keep the order, you could create an <code>Attribute</code> on the field/property which could keep this information. See below:</p> <pre><code>class OrderAttribute : Attribute { public int Order { get; private set; } public OrderAttribute(int order) { this.Order = order; } } class A { public string FieldWithoutOrder; [Order(3)] public string Field1; [Order(2)] public string Field2; [Order(1)] public string Field3; } static void Main(string[] args) { A myA = new A() { Field1 = "rofl", Field2 = "lol", Field3 = "omg", FieldWithoutOrder = "anarchy" }; var obj = (from prop in typeof(A).GetFields(BindingFlags.Public | BindingFlags.Instance) orderby prop.GetCustomAttributes(typeof(OrderAttribute),true).Select(att=&gt;((OrderAttribute)att).Order).DefaultIfEmpty(Int32.MaxValue).First() select prop.GetValue(myA) ).ToArray(); var obj2 = (from prop in typeof(A).GetProperties(BindingFlags.Public | BindingFlags.Instance) select prop.GetValue(myA,null)).ToArray(); Debug.Assert(obj[0] == "omg"); //Field3 Debug.Assert(obj[1] == "lol"); //Field2 Debug.Assert(obj[2] == "rofl"); //Field1 Debug.Assert(obj[3] == "anarchy"); //FieldWithoutOrder } </code></pre> <p>The <code>DefaultIfEmpty</code> specifies a value in case the field doesn't have the <code>Order</code> Attribute.</p> <p>This way it'll be easy for you to maintain (if you need to change the order of the fields, just change in the attribute)</p> <p>I didn't do it for <code>obj2</code> but you can paste the same <code>orderby</code> statement and it'll work.</p> <p>Hope it helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload