Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing object properties from string representations
    primarykey
    data
    text
    <p>I've got a custom object (example only code for ease of understanding) ...</p> <pre><code>public class MyClass { private string name; private int increment; private Guid id; public string Name { get { return name; } set { name = value; } } public int Increment { get { return increment; } set { increment = value; } } public Guid Id { get { return id; } set { id = value; } } } </code></pre> <p>... and a custom collection of this class ...</p> <pre><code>public class MyClassCollection : Collection&lt;MyClass&gt; { } </code></pre> <p>I was looking to write a Sort routine for the collection which will have the following public method ...</p> <pre><code> public void Sort(params string[] sortProperties) { if (sortProperties == null) { throw new ArgumentNullException("sortProperties", "Parameter must not be null"); } if ((sortProperties.Length &gt; 0) &amp;&amp; (Items.Count &gt; 1)) { foreach (string s in sortProperties) { // call private sort method Sort(s); } } } </code></pre> <p>... and the private Sort method would take a parameter of the property name ...</p> <pre><code> private void Sort(string propertyName) { } </code></pre> <p>What I want to do is be able to pass in a set of property names into the method ...</p> <pre><code> MyClassCollection current = new MyClassCollection(); // setup a objects in the collection current = GetCollectionData(); // sort by Name, then by Increment current.Sort("Name", "Increment"); </code></pre> <p>Using the property names passed into the method I want to be able to check to see if it has a property of that name, if so work out what type it is and then run through a sort of it.</p> <p>The interim workaround which I have currently got is ...</p> <pre><code> private void Sort(string propertyName) { // convert to List List&lt;MyClass&gt; myCurrentClass = Items as List&lt;MyClass&gt;; // sort if (myCurrentClass != null) { switch (propertyName) { case "Name": myCurrentClass.Sort(delegate(MyClass myClassOne, MyClass myClassTwo) { return Comparer&lt;string&gt;.Default.Compare(myClassOne.Name, myClassTwo.Name); } ); break; case "Increment": myCurrentClass.Sort(delegate(MyClass myClassOne, MyClass myClassTwo) { return Comparer&lt;int&gt;.Default.Compare(myClassOne.Increment, myClassTwo.Increment); }); break; } } } </code></pre> <p>... but ideally I would like to switch on the underlying type of the Property (string, int etc.) and using a distinct number of delegate calls for the types for sorting. I've looked around but I've not found anything which points me in the right direction. I've had a look at reflection but I couldn't see anything which would be able to help me.</p> <p>Is this even possible? and if so, how?!</p> <p>Cheers!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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