When calling the Razor.Parse or Razor.Run with an object that is an interface or an abstract class, it does not use the generic type of the object. Can you expose another method for this, or handle appropriately?
We had to do something like this to get it to work:
return (string) typeof (Razor).GetMethods()
.Where(m => m.Name == "Run" && m.ContainsGenericParameters)
.First()
.MakeGenericMethod(model.GetType())
.Invoke(null, new[] {(object) model, compiledTemplateName});
Sample Test:
[Test]
public void Renders_correctly_when_model_is_supplied_as_base_type_or_interface()
{
_templateLoader.Stub(m => m.Load("ParentTemplate")).Return("Parent Template -- @Include(\"ChildTemplate\", Model.Val2) --");
_templateLoader.Stub(m => m.Load("ChildTemplate")).Return("Child Template and @Model");
var message = new MailMessage("test1@glgroup.com", "Test2@glgroup.com", "Test3@glgroup.com", "");
try
{
_emailer.SendEmail(message, (IEmailModel)new TestEmailModel(), "ParentTemplate");
}
catch (Exception e)
{
if (e is TemplateCompilationException)
{
Console.WriteLine(string.Join(",", ((TemplateCompilationException)e).Errors));
}
else
{
Console.WriteLine(e);
}
}
Assert.That(message.Body, Is.EqualTo("Parent Template -- Child Template and It's a val2 --"));
}