thanks for reading. I am trying to write a little parser. What I am trying to do is the following. The database contains 3 tables. One with Persons (Name, LastName, Age), one with TextTemplates (“This document belongs to .”) and one with TemplateElements (for example , …). So the user is able to choose a TextTemplate, edit it and add more TemplateElements. As he presses a button the system should generate PDF documents by replacing the TemplateElements with the corresponding properties of the persons out of the Persons table. The problem is to get the persons property that matches the TemplateElement. Of course I could write some:
foreach(element...){
if(element.Equals("<Name>"))
text.Replace("<Name>", Person.Name);
if(element.Equals("<LastName>"))
text.Replace("<LastName>", Person.LastName);
}
but I want to keep this as dynamic as possible. Properties and TemplateElements could change in the future. So the best solution would be to somehow get the corresponding property according to the actual element.
Would be very nice if one of you guys has a solution for this.
Thanks 😉
Have a look at these blog posts where some implementations of ‘named formatters’ are discussed:
Essentially, the idea is that you define an extension method on
stringthat allows you to format a string based on a syntax like{PropertyName}Example: