[英]Generics - Type parameter declaration must be an identifier not a type
On the first line, I get this compilation error. 在第一行,我收到此编译错误。 "Type parameter declaration must be an identifier not a type.".
“类型参数声明必须是标识符而不是类型。” Is there a way to fix this?
有没有办法解决这个问题?
public class ExtJsGridJsonModel<IEnumerable<T>>
{
[DataMember(Name = "total")]
public int Total { get; set; }
[DataMember(Name = "rows")]
public IEnumerable<T> Rows { set; get; }
public ExtJsGridJsonModel(IEnumerable<T> rows, int total)
{
this.Rows = rows;
this.Total = total;
}
}
Update: 更新:
Sorry for the lack of detail in my question and intentions. 很抱歉,我的问题和意图不够详细。 Basically, my end goal is to do this:
基本上,我的最终目标是这样做:
new ExtJsGridJsonModel<Company>();
rather than this: 而不是这样:
new ExtJsGridJsonModel<IEnumerable<Company>>();
Basically, I want to reduce code by omitting the IEnumerable type. 基本上,我想通过省略IEnumerable类型来减少代码。 How do i do this?
我该怎么做呢?
Just take out the IEnumerable
part of your declaration: 只需删除声明的
IEnumerable
部分:
public class ExtJsGridJsonModel<T>
{
[DataMember(Name = "total")]
public int Total { get; set; }
[DataMember(Name = "rows")]
public IEnumerable<T> Rows { set; get; }
public ExtJsGridJsonModel(IEnumerable<T> rows, int total)
{
this.Rows = rows;
this.Total = total;
}
}
I assume that you would be using this to store what is essentially a two dimensional set of values: 我假设您将使用它来存储本质上是二维值的集合:
public class ExtJsGridJsonModel<T> where T : IEnumerable
{
[DataMember(Name = "total")]
public int Total { get; set; }
[DataMember(Name = "rows")]
public IEnumerable<T> Rows { set; get; }
public ExtJsGridJsonModel(IEnumerable<T> rows, int total)
{
this.Rows = rows;
this.Total = total;
}
}
If not, or T
is actually a strongly-typed row class, then you can remove the where T : IEnumerable
clause 如果不是,或者
T
实际上是强类型的行类,则可以删除where T : IEnumerable
子句
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.