Note that there are some explanatory texts on larger screens.

plurals
  1. PO.Net Get Property Values of Object by Key, to any depth
    text
    copied!<p>I would like to be able to access the value of an object property to any depth having only the string-key of the property. Also, if possible, using collection indexing on List properties. So, If I have the string "Person.Surname" then I could get the value "Smith" from and instanciated CaseConductor object. So given some setup code like this ...</p> <pre><code>//- 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 ... object val = caseConductor.SomeCleverFunction("Person.Surname"); // or this ... object val = caseConductor.SomeCleverFunction("CaseNoteList[0].NoteText"); </code></pre> <p>Has anyone done this before ? Here are some setup classes ...</p> <pre><code>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> <p>Our use case is to iterate over a series of appropriately named content controls in a word dcoument template using open xml sdk 2, and poke values into a newly created word documents, something like this ...</p> <pre><code>List&lt;SdtElement&gt; ccList = wordprocessingDocument.MainDocumentPart.Document.Descendants&lt;SdtElement&gt;().ToList(); foreach (var cc in ccList) { string alias = cc.SdtProperties.GetFirstChild&lt;SdtAlias&gt;().Val.Value; switch (cc.GetType().Name) { case "SdtRun": SdtRun thisRun = (SdtRun)cc; //thisRun.Descendants&lt;Text&gt;().First().Text = theValueToBePoked ; break; } } </code></pre>
 

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