Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm assuming that caseConductor.SomeCleverFunction is not a static method, and has access to the Person object, and that the Person object itself if a public property.</p> <p>I'm also assuming that you want to pass a string like "prop.address.street" where each sub property is an class that containts a puplic property with that name</p> <ol> <li>Split the string input on the period, find the left most string</li> <li>Use reflection to get a list of properties ( typeof(caseconductor).GetProperties() )</li> <li>Find the matching property, call GetValue on it, passing the last known solid object (starting with 'this') and storing a refernce to it.</li> <li>if there is more sub properties in the string left, repeat to step 1, removing the left most part of the string.</li> <li>otherwise, call GetValue() on the property, using the last GetValue() return object from step 3, and return it.</li> </ol> <p>Something like:</p> <p>"prop.address.street" -> find property "prop" from 'this' and GetValue, </p> <p>there is still more "."'s so repeat, storing return value</p> <p>"address.street" -> find property "address" from the last returned GetValue, and get it's value.</p> <p>there is still more "."'s so repeat, storing return value</p> <p>"street" -> find property "street" from the last returned GetValue, and return it's value.</p> <p>End of string, return last value</p> <p>Edit - </p> <p>This is pretty rough, but toss it into LinqPAD and take a look.</p> <p><a href="http://www.linqpad.net/" rel="nofollow noreferrer">http://www.linqpad.net/</a></p> <p>Edit #2 - you should be able to index into arrays using the ^ syntax below.</p> <p>Again this is reaaaaaaaaally rough, just enough to get a working example.</p> <p>Edit #3 - Cleaned up the example slightly and changed it from my example classes to yours.</p> <pre><code>void Main() { //- Load a caseConductor var caseConductor = new CaseConductor(); caseConductor.CaseID = "A00001"; // person caseConductor.Person = new Person(); caseConductor.Person.Surname = "Smith" ; caseConductor.Person.DOB = DateTime.Now ; // case note list caseConductor.CaseNoteList = new List&lt;Note&gt;(); caseConductor.CaseNoteList.Add(new Note { NoteText = "A-1" , NoteDt = DateTime.Now }); caseConductor.CaseNoteList.Add(new Note { NoteText = "B-2", NoteDt = DateTime.Now }); // I could do this ... string val1 = caseConductor.GetPropertyValue&lt;string&gt;("Person.Surname"); // or this ... Note val2 = caseConductor.GetPropertyValue&lt;Note&gt;("CaseNoteList^1"); val1.Dump("val1"); //this is a string val2.Dump("val2"); //this is a Note } public static class extensions { public static T GetPropertyValue&lt;T&gt;(this object o,string Properties) where T:class { var properties = Properties.Split('.'); var indexsplit = properties[0].Split('^'); var current = indexsplit[0]; var prop = (from p in o.GetType().GetProperties() where p.Name == current select p).Take(1).Single(); var val = prop.GetValue(o,null); if(indexsplit.Length&gt;1) { var index = int.Parse(indexsplit[1]); IList ival = (IList)val; val = ival[index]; } if(properties[0] == Properties) return (T)val; else return val.GetPropertyValue&lt;T&gt;(Properties.Replace(properties[0]+".","")); } } class Note { public Guid NoteID { get; set; } public string NoteText { get; set; } public DateTime? NoteDt { get; set; } } public class Person { public Guid PersonID { get; set; } public string Surname { get; set; } public string Forename { get; set; } public DateTime? DOB { get; set; } } class CaseConductor { public String CaseID{get;set;} public Person Person { get; set; } public List&lt;Note&gt; CaseNoteList { get; set; } } </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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