简体   繁体   English

C#泛型,我做错了什么?

[英]C# Generics, what am I doing wrong?

I'm some what new to Generics and I can't figure out why the following doesn't work. 我是Generics的新手,我无法弄清楚为什么以下不起作用。

I have an extension to IEnumerable<T> , called Grid and it looks like so 我有一个IEnumerable<T>的扩展名,叫做Grid,它看起来像这样

  public static class IEnumberableGridExtension
  {
        public static HelperResult Grid<T>(this IEnumerable<T> gridItems, Action<GridView<T>> thegrid) 
        {
            ........
        }

   }

Say, I have a variable in my razor Model called "Products" and it is the type List<Product> , so I tried to do 说,我的剃刀模型中有一个名为“Products”的变量,它是List<Product>类型,所以我试着做

@Model.Products.Grid<Product>(grid=>{
...
});

It states "Cannot convert method group 'Grid" to non-delegate type 'object'. 它声明“无法将方法组'Grid'转换为非委托类型'对象'。 Did you intend to invoke the method?", with "@Model.tasks.Grid" red underlined. 你打算调用这个方法吗?“,用”@ Model.tasks.Grid“红色加下划线。

The funny thing is, visual studio compiles, everything is fine. 有趣的是,视觉工作室编译,一切都很好。 Of course, if i simply do 当然,如果我只是这样做

@Model.Products.Grid(grid=>{
...
});

It's all fine. 一切都很好。

The Razor parser is interpreting < and > as normal HTML tags. Razor解析器将<和>解释为普通的HTML标记。 You can avoid the problem wrapping the call in parenthesis: 您可以避免在括号中包含调用的问题:

@(Model.Products.Grid<Product>(grid=>{
...
}))

You can find additional info here: How to use generic syntax inside a Razor view file? 您可以在此处找到其他信息: 如何在Razor视图文件中使用通用语法?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM