I cannot access to dynamic object properties in Razor Template
I created a dynamic object like below:
dynamic myObject = new
{
DisplayName = "Mahesh开发者_如何学C"
};
Content = Parse("Main", myObject);
Then I parsed it for Razor template. But it doesnt work because of the object cannot access to its properties. What is the problem here ?
Thanks in advance,
I did it by this way and it works. But I want to know why the way I did first is not working
dynamic FooterModel = new ExpandoObject();
FooterModel.DisplayName= "Kaplan";
The anonymous type has some accessibility issues, in that dynamic
via the c# provider tries to respect accessibility. An anonymous type in a different module is not accessible (and remember that MVC pages will typically compile into a different module), hence no properties.
IIRC, however, this is actually fixed in a later MVC patch - I seem to remember hitting this when my local machine was a rev higher than our dev-server, meaning: it worked locally on the higher revision, but failed as you describe on the dev-server.
The ExpandoObject
does not suffer this because it does not use the c# provider (it implements IDynamicMetaObjectProvider
instead), and has no concept of accessibility (it simply maps member-names to the dictionary).
精彩评论