Okay, so I want to have a generic method that can read data from an unknown data class given a razor type property pointer.
I.E. I want to be able to pass in a string containing a template and a object and then extract the data from the object given the string in the template.
String template = "@Model.Basket";
var results = parse(template, data);
the parse method should have no knowledge of the specific model being passed to it, so its method signature should be something like:
public var parse(String template, object model)
where data is something like
public class Receipt
{
public String Firstname { get; set; }
public String Surname { get; set; }
public DateTime TransactionDateTime { get; set; }
public Boolean ReturnCustomer { get; set; }
public LineItem[] Basket { get; set; }
public class LineItem
{
public Product product { get; set; }
public int Units { get; set; }
public class Product
{
public int ProductId { get; set; }
public String ProductName { get; set; }
public double Cost { get; set; }
}
}
}
In this case parse should return an array of line items, but if my template was equal to “@Model.Firstname” it should simply return a string containing the the name stored in that property.
Does anyone have any good ideas.
Note that for this given solution, I can not make use of the Razor engine as my templates are not in a format that the razor engine can understand.
If you want to do this, you want to look at the
System.Reflectionnamespace – in particular, PropertyInfo. I’m not sure that it is a good idea, though.