RazorEngine icon indicating copy to clipboard operation
RazorEngine copied to clipboard

Subclassed models don't carry through to the Layout

Open korggy opened this issue 14 years ago • 3 comments

It's easiest to describe this bug via an example...

The following works:

public class Model {}

View.cshtml: @model Model @{ _Layout = "Layout"; } ...

Layout.cshtml: @model Model ...

The following doesn't work:

public class Model {} public class Model2 : Model {}

View.cshtml: @model Model2 @{ _Layout = "Layout"; } ...

Layout.cshtml: @model Model ...

The first example works fine but when I try to parse the 2nd example, I get the following exception:

System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object. Source=l1lqrdfo StackTrace: at CompiledRazorTemplates.Dynamic.cddfefecbdb.Execute() at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context) at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context) at RazorEngine.Templating.TemplateService.Run(ITemplate template) at RazorEngine.Templating.TemplateService.Parse[T](String razorTemplate, T model, String name) at ConsoleApplication2.Program.Main4() in d:\Temp\ConsoleApplication2\Program.cs:line 62 at ConsoleApplication2.Program.Main(String[] args) in d:\Temp\ConsoleApplication2\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

korggy avatar Jan 30 '12 19:01 korggy

The problem appears to be in TemplateService::SetModel, and in particular the following code

var staticModel = template as ITemplate; if (staticModel != null) staticModel.Model = model;

This doesn't work because of this: http://stackoverflow.com/questions/245607/how-is-generic-covariance-contra-variance-implemented-in-c-sharp-4-0/246101#246101

Basically I think what you need to do is grab the model type implemented by the template and see if the current model can be assigned to it. Something like this:

var type = template.GetType(); if (type.BaseType.GetGenericArguments()[0].IsAssignableFrom(typeof(T))) { // Do some magic here to set the model on the template, possibly via reflection...? }

korggy avatar Jan 30 '12 20:01 korggy

Ok, I managed to solve this via some reflection. It's not pretty but it works.

I replaced this

var staticModel = template as ITemplate<T>;
if (staticModel != null)
  staticModel.Model = model;

with this

var type = template.GetType();
if (type.BaseType.IsGenericType
  && type.BaseType.GetGenericArguments()[0].IsAssignableFrom(typeof(T)))
{
  var propertyInfo = type.BaseType.GetProperty("Model");
  propertyInfo.SetValue(template, model, null);
}

Apologies for not submitting a patch for this, but I'm not a Git user...

korggy avatar Jan 30 '12 20:01 korggy

I see what you are describing. Will investigate.

Antaris avatar Jan 31 '12 16:01 Antaris