This is a bug in RazorEngine: the Razor.Compile
works on TemplateBase<dynamic>
(so Model
and everything derived from it is dynamic
too) and that means that no extension methods undergo the 'compiler-magic' to convert them to the static calls. Then Razor.Run
passes the Model
as the correct type, but the extension method syntax is called as an instance method.
There will probably be a fix for this soon (the bug's only a few days old and this is a corner case), but in the meantime I have a workaround: explicitly type the Model
in the Razor template
@using MyCompany.Extensions
@using MyCompany
@{
ExpectedModelClass strongTypeModel = Model as ExpectedModelClass;
MyClass myInstance = new MyClass(strongTypeModel , ...);
}
Some text @myInstance.ExtensionMethod() some more text
This now works, because even though Model
is still dynamic
at compile-time that doesn't spread to myInstance
any more.
It's not ideal, and everywhere I used Model
now has to be strongTypeModel
, but that's a much simpler substitution.