Is this a way to call the model dynamically in MVC 4? (Description inside)-Collection of common programming errors
When the Razor template gets compiled, @(...)
expression is replaced by the call to Write
inside the generated Execute
method so your example would compile into the following:
public void Execute()
{
// ...
dynamic r = base.Model.R;
Write((dynamic)base.Model.R);
}
Since Write
must accept a parameter of type object
, anything can be passed (so I’m not sure you need a cast to dynamic
here).
Here’s an article that can help you understand how Razor templates are processed: Leveraging Razor Templates Outside of ASP.NET.
As for dynamic
keyword, this basically tells the compiler that the expression of this type will be interpreted at runtime (late binding). Here’s some more information: Using Type dynamic (MSDN).
Hope this clarifies things a bit for you.