asp.net MVC 3剃须刀视图 - >元组的问题强类型列表 [英] asp.net mvc 3 razor view -> strongly typed List of tuple problem

查看:260
本文介绍了asp.net MVC 3剃须刀视图 - >元组的问题强类型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与asp.net MVC的Razor视图一个奇怪的问题。

I'm having an odd problem with asp.net MVC razor view.

我希望我的模型是一个列表与LT元组LT;字符串,INT,INT,INT,INT>> 这是我在其他的C#方法非常有效。但是,当我将其粘贴到 @model 声明似乎只挑出元组的字符串的一部分。所以,我没有整数。只有ITEM1。

I want my model to be a List<Tuple<string, int, int, int, int>> which is perfectly valid in my other c# methods. But when I paste it into the @model declaration it seems to only pick out the string part of the tuple. So I have no ints. Only item1.

这问题不是present如果我把它绑定到一个元组,而不是名单。

This problem is not present if I make it bind to a Tuple instead of the List.

好像产生code是错误的,所以也许这就是剃须刀视图中的错误?

Seems like the generated code is wrong, so perhaps this is a bug in razor view?

我在编译得到的错误是:

The error I get at compilation is:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1003: Syntax error, '>' expected

Source Error:


Line 27:     
Line 28:     
Line 29:     public class _Page_Views_Dashboard_DestinationStatistics_cshtml : System.Web.Mvc.WebViewPage<List<Tuple<string {
Line 30:         
Line 31: #line hidden

要隔离这个问题,我做了以下事情:

To isolate this problem I did the following thing:

创建一个空的asp.net MVC项目。创建一个新的观点。过去以下code。

Create an empty asp.net mvc project. Create a new view. Past the following code.

@model List<Tuple<string, int, int, int, int>>

@foreach (var stat in Model)
{
    <tr>
        <td>
            @stat.Item1
        </td>
        <td>
            @stat.Item2
        </td>
        <td>
            @stat.Item3
        </td>
        <td>
            @stat.Item4
        </td>
        <td>
            @stat.Item5
        </td>
    </tr>
}

我知道我可以只创建一个类或结构来保存数据,而不是。只是要求出于好奇

I know I can just create a class or a struct to hold the data instead. Just asking out of curiosity

编辑:
解决并上报MVC团队在这里的http://aspnet.$c$cplex.com/workitem/8652

推荐答案

编辑我已经修剪了这里的一些正在进行中的注释 - 只需查看历史记录看

所以,你可以用1,2,3或4元组泛型参数这项工作,但它不与5.只要你使用5个参数工作,它产生code是这样的:

So you can make this work with 1, 2, 3 or 4 tuple generic parameters but it doesn't work with 5. As soon as you use 5 parameters it generates code like this:

 public class _Page_Views_Home_Index_cshtml : 
   System.Web.Mvc.WebViewPage<List<System.Tuple<string {

我想只是觉得,如果它是一个字符长度的限制,所以我生成这样一个类:

I wanted to just find out if it's a character-length limitation, so I generated a class like this:

namespace ASP{  //same namespace that the backend code for the page is generated
  public class T { } 
}

和改变模型的声明:

@model List<Tuple<T,T,T,T,T>>.

在结束(见历史),我得

In the end (see the history) I got to

@inherits System.Web.Mvc.WebViewPage<Tuple<T,T,T,T,T>>

同样的问题!这不是与@model关键字一个问题...

Same problem! It's not a problem with the @model keyword...

过了一段时间(通过MVC3和剃刀源读取,增加了一些测试来该解决方案) - 但这里有一个测试,显示了为什么我们得到这个错误:

It took a while (reading through the MVC3 and Razor source, adding a couple of tests to that solution) - but here's a test that shows the why we get this error:

[TestMethod]
public void TestMethod()
{
  System.CodeDom.CodeTypeReferenceCollection c = 
    new CodeDom.CodeTypeReferenceCollection();
  c.Add("Tuple<T,T,T,T>");
  c.Add("Tuple<T,T,T,T,T>");
  //passes
  Assert.AreEqual("Tuple<T,T,T,T>", c[0].BaseType);
  //fails
  Assert.AreEqual("Tuple<T,T,T,T,T>", c[1].BaseType);    
}

所以 - 四参数版本传递,而不是5参数版本

So - the four-parameter version passes, but not the 5 parameter version.

和猜测乜实际值元组LT;吨 - 即截断泛型类型名的截断正是您已在$所观察到的同样的方式C $ç

And guess what- the actual value is Tuple<T - i.e. a truncated generic type name truncated exactly the same way that you've observed in your code.

这两种标准的剃刀解析器和MVC的剃刀解析器解析时,使用 codeTypeReferenceCollection 类型无论是 @inherits @model 关键字。这里是code生成过程中code为 @inherits

Both the standard Razor parser and the Mvc Razor parser use the CodeTypeReferenceCollection type when parsing either the @inherits or @model keyword. Here's the code for @inherits during code generation:

protected internal virtual void VisitSpan(InheritsSpan span) {
  // Set the appropriate base type
  GeneratedClass.BaseTypes.Clear();
  GeneratedClass.BaseTypes.Add(span.BaseClass);

  if (DesignTimeMode) {
    WriteHelperVariable(span.Content, InheritsHelperName);
  }
}

GeneratedClass.BaseTypes codeTypeReferenceCollection - 和 span.BaseClass 是一个字符串。下面,通过在伊尔根,有问题的方法必须是私有方法 codeTypeReference.Initialize(字符串的typeName,codeTypeReferenceOptions选项)。我已经没有足够的时间,现在弄清楚为什么它打破了 - 但是这是一个微软开发者的工作,我认为:) 下面更新 - 无法抗拒。现在我知道这是错的

GeneratedClass.BaseTypes is a CodeTypeReferenceCollection - and span.BaseClass is a string. Following that through in ILGen, the offending method must be the private method CodeTypeReference.Initialize(string typeName, CodeTypeReferenceOptions options). I've not enough time now to figure out why it breaks - but then that's a Microsoft developer's job I think :) Update below - couldn't resist. I now know where it's wrong

您不能使用泛型与任何剃刀 @inherits @model 语句超过4个参数(至少在C#中 - 不知道VB)。看来,剃刀分析器错误地使用 codeTypeReference 键入

You can't use generics with more than 4 parameters in either Razor @inherits or @model statements (at least in C# - don't know about VB). It appears that the Razor parser is incorrectly using the CodeTypeReference type.

的事情之一是 codeTypeReference 确实是剥离从通过型式名称程序集名称信息与该方法的调用 codeTypeReference.RipOffAssemblyInformationFromTypeName(字符串的typeName)

One of the things that CodeTypeReference does is strip off assembly name information from a passed type name with a call to the method CodeTypeReference.RipOffAssemblyInformationFromTypeName(string typeName).

当然,如果你仔细想想 - 元组LT; T,T,T,T,T&GT; 就像一集限定的类型名称:随着类型名称= 元组LT;吨,大会= T ,版本= T ,文化= T 公钥= T (如果你写了一个非常糟糕的C#语法分析器!)。

And of course, if you think about it - Tuple<T,T,T,T,T> is just like an assembly-qualified type name: With the type name = Tuple<T, Assembly = T, Version=T, Culture=T, PublicKeyToken=T (if you write a really BAD C# parser!).

果然 - 如果你在​​元组下通行; T,T,T,T,T,T&GT; 作为类型名称 - 你实际上得到一个元组LT; T,T&GT;

Sure enough - if you pass in Tuple<T,T,T,T,T,T> as the type name - you actually get a Tuple<T,T>.

展望深入到code,它催芽接收语言无关的类型名称(句柄'[',但没有对'&LT;'为例),因此,实际上,MVC开发团队不应该仅仅是移交从我们的源C#的类型名称直通。

Looking deeper into the code, it's primed to receive a language-neutral typename (handles '[' but nothing for '<', for example) so, actually, the MVC team shouldn't just be handing the C# typename from our source straight through.

的MVC开发团队需要改变他们如何产生的基本类型 - 他们可以使用公共codeTypeReference(字符串的typeName,则params codeTypeReference [] typeArguments)构造一个新的参考(而不是仅仅依靠。新增(span.BaseClass)创建它) ,并解析泛型参数本身,因为他们知道,类型名称将是C#/ VB的风格 - 而不是语言中立的.Net风格的托架等作为实际名称的一部分

The MVC team needs to change how they generate the base type - They could use the public CodeTypeReference(string typeName, params CodeTypeReference[] typeArguments) constructor for a new reference (instead of just relying on the .Add(span.BaseClass) creating it), and parse the generic parameters themselves since they know that the type name will be C#/VB style - not language-neutral .Net style with brackets etc as part of the actual name.

这篇关于asp.net MVC 3剃须刀视图 - &GT;元组的问题强类型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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