c# - Accessing object properties from string representations -


I have a custom object (for example, code for easy understanding) ...

  public class MyClass {name of private string; Private Incharge; Private Guide ID; Public string name {get {return name; } Set {name = value; }} Public Int Increment {Return Salary Increment {Receive; } Set {increment = value; }} Public Guid ID ({return id;} set {id = value}}}  

... and a custom collection of this class ...

  Public class MyClassCollection: collection  {}  

I wanted to write a sort routine for the collection which would be the following public law ...

  public zero sort (parameter string [] sort proportions) {if (sort properties == empty) {fix minimum to new argument ("sort properties", "parameter is not empty Should be ");} if ((sort properties) gt; 0) & amp; amp; amp; item (item & gt; 1)) {foreach (strings in sort properties) {// call private sort method Sorting;}}}  

... and the Private Sort Method property will take a parameter name ...

  Sort Private Zero (string propertyName } {}  

What I want to do, can pass in a set of property names in the law ...

  MyClassCollection current = new MyClassCollection (); // Collection Turn an object = GetCollectionData (); // Sort by name, then incrementally turned on. ("Name", "increase");  

Using property names passed in the law I want to be able to see if there is the property of that name, if this works, then how is it and then < / P>

Sort Private Zero (string propertyName) {// Change to list list & lt; MyClass & gt; MyCurrentClass = Item as List & lt; MyClass & gt; // sort if (myCurrentClass! = Null) {switch (propertyName) {case "name": myCurrentClass.Sort (Representative (MyClass myClassOne, MyClass myClassTwo) {Return Comparer & lt; String & gt ;.Default.Compare (myClassOne Name, myClassTwo Name);}); break; Case "increase": myCurrentClass.Sort (Representative (MyClass myClassOne, MyClass myClassTwo) {Return Comparer & lt; int & gt; default.Compare (myClassOne.Increment, myClassTwo.Increment);}); break; }}}

... but ideally I would like to switch to the underlying type of property (string, ink etc.) and use a different number of representative calls for these types. Do sorting I have looked around, but I did not find anything that tells me in the right direction. I have seen the reflection but I can not see anything that would be able to help me.

Is this possible? And if so, how ?!

Cheers!

To get the reflection route - type . GetProperty (name of string) . After that, making the right comparator may be difficult - you want to write a common method, and then using that type, with reflection, depending on the type of property. It all goes very fast, I'm afraid - but it's definitely viable.


Comments