How to see which model properties cause problem with RazorEngine

12 Views Asked by At

I use RazorEngine to substitute some tags in a text with values from a model. All works fine, when the model is totally populated. But I want it to be lazily populated, meaning only the properties which are required in text should be populated.

How do I know if advance, which properties of a model are required in text? For example:

public class SubModel
{
    public int Prop { get; set; }
}

public class MyModel
{
    public int Prop { get; set; }
    public SubModel SubModel { get; set; }
}

internal class Program
{
    static void Main(string[] args)
    {
        var razor = new RazorEngine();
        var subModel = new SubModel
        {
            Prop = 123,
        };

        var model = new MyModel
        {
            Prop = 456,
            SubModel = null, // sub model not initialized 
        };

        var templ = razor.Compile("Model Prop: @Model.Prop, SubModel Prop: @Model.SubModel.Prop");
        var result = templ.Run(model);

        // here I would like to know which properties were not setup (SubModel in this case)
        // to lazy load them and Run again with updated model
        // instead RuntimeBinderException is thrown which does not provide enough info

        // something like: "SubModel is null"
        model.SubModel = subModel;
        result = templ.Run(model);
    }
}
0

There are 0 best solutions below