Dynamic Anonymous type in Razor causes RuntimeBinderException?

Anonymous types having internal properties is just one of the rubbish decision M$ made Here is a quick and nice extension to fix this problem i.e. By converting the anonymous object into an ExpandoObject right away public static ExpandoObject ToExpando(this object anonymousObject) { IDictionary anonymousDictionary = HtmlHelper. AnonymousObjectToHtmlAttributes(anonymousObject); IDictionary expando = new ExpandoObject(); foreach (var item in anonymousDictionary) expando.

Add(item); return (ExpandoObject)expando; } It's very easy to use: return View("ViewName", someLinq. Select(new { x=1, y=2}.ToExpando()) Of course in your view: foreach (var item in Model) { x = @item. X, y = @item.

Y } NET Solution provided by http://www.dotnetwise.com.

Anonymous types having internal properties is just one of the rubbish decision M$ made. Here is a quick and nice extension to fix this problem i.e. By converting the anonymous object into an ExpandoObject right away.

Public static ExpandoObject ToExpando(this object anonymousObject) { IDictionary anonymousDictionary = HtmlHelper. AnonymousObjectToHtmlAttributes(anonymousObject); IDictionary expando = new ExpandoObject(); foreach (var item in anonymousDictionary) expando. Add(item); return (ExpandoObject)expando; } It's very easy to use: return View("ViewName", someLinq.

Select(new { x=1, y=2}.ToExpando()); Of course in your view: @foreach (var item in Model) { x = @item. X, y = @item. Y } .

NET Solution provided by http://www.dotnetwise.com.

Great! Thanks for sharing :-) – Adrian Grigore Apr 15 at 15:39 +1 I was specifically looking for HtmlHelper. AnonymousObjectToHtmlAttributes I knew this absolutely had to baked in already and didn't want to reinvent the wheel with similar handrolled code.

– Chris Marisic May 11 at 21:37 What is the performance like on this, compared to simply making a strongly typed backing model? – GONeale May 25 at 1:00 @DotNetWise, Why would you use HtmlHelper. AnonymousObjectToHtmlAttributes when you can just do IDictionary anonymousDictionary = new RouteDictionary(object)?

– Jeremy Jun 22 at 19:29 I have tested HtmlHelper. AnonymousObjectToHtmlAttributes and works as expected. Your solution can also work.

Use whichever seems easier :) – DotNetWise Jun 25 at 8:46.

I found the answer in a related question. The answer is specified on David Ebbo's blog post Passing anonymous objects to MVC views and accessing them using dynamic The reason for this is that the anonymous type being passed in the controller in internal, so it can only be accessed from within the assembly in which it’s declared. Since views get compiled separately, the dynamic binder complains that it can’t go over that assembly boundary.

But if you think about it, this restriction from the dynamic binder is actually quite artificial, because if you use private reflection, nothing is stopping you from accessing those internal members (yes, it even work in Medium trust). So the default dynamic binder is going out of its way to enforce C# compilation rules (where you can’t access internal members), instead of letting you do what the CLR runtime allows.

Beat me to it :) I ran into this problem with my Razor Engine (the precursor to the one on razorengine.codeplex. Com ) – BuildStarted Feb 25 at 17:28 This is not really an answer, not saying more about the "accepted answer"! – DotNetWise Apr 18 at 21:47 1 @DotNetWise: It explains why the error ocurrs, which was the question.

You also get my upvote for providing a nice workaround :) – Lucas Apr 30 at 20:09.

You can use the framework impromptu interface to wrap an anonymous type in an interface. You'd just return an IEnumerable and at the end of your Linq use .AllActLike(); this works because it calls the anonymous property using the DLR with a context of the assembly that declared the anonymous type.

Awesome little trick :) Don't know if it is any better than just a plain class with a bunch of public properties though, at least in this case. – Andrew Backer Sep 19 at 6:50.

Wrote a console application and add Mono. Cecil as reference (you can now add it from NuGet), then write the piece of code: static void Main(string args) { var asmFile = args0; Console. WriteLine("Making anonymous types public for '{0}'.", asmFile); var asmDef = AssemblyDefinition.

ReadAssembly(asmFile, new ReaderParameters { ReadSymbols = true }); var anonymousTypes = asmDef. Modules . SelectMany(m => m.

Types) . Where(t => t.Name. Contains("f__AnonymousType")); foreach (var type in anonymousTypes) { type.

IsPublic = true; } asmDef. Write(asmFile, new WriterParameters { WriteSymbols = true }); } The code above would get the assembly file from input args and use Mono. Cecil to change the accessibility from internal to public, and that would resolve the problem.

We can run the program in the Post Build event of the website. I wrote a blog post about this in Chinese but I believe you can just read the code and snapshots. :).

Anonymous types having internal properties is just one of the rubbish decision M$ made.

I found the answer in a related question. The answer is specified on David Ebbo's blog post Passing anonymous objects to MVC views and accessing them using dynamic.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions