匿名类型缺少的成员问题的动态视图 - MVC3 [英] Dynamic view of anonymous type missing member issue - MVC3

查看:97
本文介绍了匿名类型缺少的成员问题的动态视图 - MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC3的网站,我设置测试另一个网站 - 大部分已经快速和肮脏的,所以我没有去镇上所有视图创建模型和视图模型类型 - 只有在输入从用户requried

I have an MVC3 site that I've setup for testing another site - most of it has been quick and dirty, and so I've not gone to town creating model and view model types for all the views - only where input is requried from the user.

好了,所以我有一个项目一个LINQ序列,并将其设置为 ViewBag 控制器方法

Okay so I have a controller method that projects a Linq sequence and sets it into ViewBag.

ViewBag.SomeData = Enumerable.Range(1,10).Select(i=> new { Value = i });

在我看来,(剃刀C#),那么我想读这 - 很简单:

In my view (Razor C#) I then want to read this - quite simple:

@foreach(dynamic item in ViewBag.SomeData)
{
  @:Number: @item.i
}

除,当然,我得到一个 RuntimeBinderException ,因为在控制器中创建匿名类型是内部的Web项目的输出程序集和实际的剃刀code在这里将通过构建管理产生不同的装配运行的话,这一切的一切被拒绝!

Except, of course, I get a RuntimeBinderException because the anonymous type created in the controller is internal to the web project's output assembly and the actual Razor code here will be running in a different assembly generated by the build manager so, all in all DENIED!

显然,一个正确的模式类型就可以解决这个问题 - 但让我们说,我根本就不想这样做,因为这是我的prerogative(!) - 如何以最佳方式保持code到最低限度这里保留了动态的烦躁?

Obviously a 'proper' model type would solve the issue - but let's say I simply don't want to do that because that's my prerogative(!) - how best to keep the code to a minimum and retain the dynamic-ness here?

推荐答案

好吧对不起,我就是不同意,这是可怕的等等等等。是的,我绝不会考虑在生产现场做这个东西 - 但对于一个原型,非商业的,仅供内部使用,用于测试功能的目的,我觉得使用这种方法是非常有效的。

Okay sorry I just don't agree that this is horrible etc etc. Yes I would never consider doing this stuff on a production site - but for a prototyping, non-commercial, internal-only, for-testing-purposes I think using this approach is perfectly valid.

这是我做了什么(我强调这一点只有真正解决了该问题充分匿名类型);起重成员并推入一个 ExpandoObject

This is what I've done (and I stress this only really addresses the issue adequately for anonymous types); lifting the members out and pushing them into an ExpandoObject.

我最初的变化,使投影多语句返回一个 ExpandoObject

My initial change was to make the projection a multi-statement that returns an ExpandoObject:

ViewBag.SomeData = Enumerable.Range(1,10).Select(i=> {
  var toReturn = new ExpandoObject();
  toReturn.Value = i;
  return toReturn;
});

这几乎是短匿名类型只是没有那么干净。

Which is almost as short as the anonymous type just not as clean.

但后来我想,如果我可能逃脱抓住公开可读成员走出匿名类型(可能依赖于编译器内部 - 类型是内部的,但它生成的属性是公共的):

But then I wondered if I could possibly get away with grabbing the publicly readable members out of the anonymous type (possibly relies on compiler internals - the type is internal, but the properties it generates are public):

public static class SO7429957
{
  public static dynamic ToSafeDynamic(this object obj)
  {
    //would be nice to restrict to anonymous types - but alas no.
    IDictionary<string, object> toReturn = new ExpandoObject();

    foreach (var prop in obj.GetType().GetProperties(
      BindingFlags.Public | BindingFlags.Instance)
      .Where(p => p.CanRead))
    {
      toReturn[prop.Name] = prop.GetValue(obj, null);
    }

    return toReturn;
  }
}

这意味着然后,我可以用我原来的code - 但在标记结束一点点扩展方法调用:

Which means I can then use my original code - but with a little extension method call tagged on the end:

ViewBag.SomeData=Enumerable.Range(1,10).Select(i=> new { Value = i }.ToSafeDynamic());

和你知道 - 我知道这是效率不高,大多数人会说这是丑陋的;但它会为我节省了时间,让我把重点放在写作特点在我的测试网站我的QA团队(整个:)当然完美无暇的code是,)测试真实的东西来使用。

And do you know what - I know it's not efficient, and most people will say it's ugly; but it'll saved me time and allow me to focus on writing features in my test site for my QA team to use in testing the real thing (whose code is, of course immaculate throughout :) ).

这篇关于匿名类型缺少的成员问题的动态视图 - MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆