简体   繁体   English

需要帮助来理解MVCContrib Grid中的这段代码

[英]Need help to understand this piece of code in MVCContrib Grid

I'm a bit new to the concept of "Action" in C# and delegate in general. 我对C#中的“动作”的概念有点陌生,通常来说都是委托的。

I'm trying to study how to build a custom html component in MVC, and I chose the grid component of MVCContrib to start. 我正在尝试研究如何在MVC中构建自定义html组件,并且选择了MVCContrib的网格组件来启动。

To add columns, typically we do 要添加列,通常我们要做

 <%= Html.Grid(Model).Columns(column =>
            {
                column.For(model => model.Date).Format("{0:d}");
                column.For(model => model.DayAmount);
                column.For(model => model.LeaveType);
            })
            %> 

and I see the source of Columns like the following 而且我看到列的来源如下

public IGridWithOptions<T> Columns(Action<ColumnBuilder<T>> columnBuilder)
        {
            var builder = new ColumnBuilder<T>();
            columnBuilder(builder);

            foreach (var column in builder)
            {
                if (column.Position == null) 
                {
                    _gridModel.Columns.Add(column);
                } 
                else
                {
                    _gridModel.Columns.Insert(column.Position.Value, column);   
                }
            }

            return this;
        }

What I'm confused of is the Action parameter In this instance, Type is CustomBuilder, so when did the "CustomBuilder" object got instantiated? 我很困惑的是Action参数,在这种情况下,Type是CustomBuilder,那么“ CustomBuilder”对象什么时候实例化的呢?

I suppose, i can rewrite the calling statement above as 我想,我可以将上面的调用语句重写为

Html.Grid(Model).Columns(delegate(CustomBuilder<T> column)
{
});

or a bit more explicit as 或者更明确

Html.Grid(Model).Columns(new Action<CustomBuilder<T>>(delegate(CustomBuilder<T> column)
                      {
                      });
);

So are we saying, when the Action was instantiated with the "new" keyword above, the param "CustomBuilder" was instantiated as well? 那么我们是说,当使用上面的“ new”关键字实例化Action时,也实例化了“ CustomBuilder”参数吗?

Lastly, in the 最后,在

" public IGridWithOptions<T> Columns(Action<ColumnBuilder<T>> columnBuilder) " public IGridWithOptions<T> Columns(Action<ColumnBuilder<T>> columnBuilder)

function, 功能,

the first two lines are 前两行是

var builder = new ColumnBuilder<T>();
columnBuilder(builder);

What do they do? 他们在做什么? Looks like it's instantiating ColumBuilder object and pass it as a parameter to Action method columBuilder. 看起来它正在实例化ColumBuilder对象,并将其作为参数传递给Action方法columBuilder。 Is this where you instantiate the parameter? 是您在其中实例化参数的位置吗?

Thank you all. 谢谢你们。

It's nothing to do with action concept. 这与行动概念无关。
The delegates is present in .net from the beginning so you should start with the first step. 代理从一开始就存在于.net中,因此您应该从第一步开始。 Should build the wall before the roof. 应该在屋顶之前建造墙。
Delegates 代表们
Lambda Expressions Lambda表达式
Expression trees 表达树
But you should know about generic classes and methods, extension methods... 但是您应该了解通用类和方法,扩展方法...

Got it after read this excellent article. 阅读这篇出色的文章后明白了。

http://www.codeproject.com/Articles/47887/C-Delegates-Anonymous-Methods-and-Lambda-Expressio http://www.codeproject.com/Articles/47887/C-Delegates-Anonymous-Methods-and-Lambda-Expressio

while it's talking about Func, the concept applies to Action, which does not return any results. 在谈论Func时,该概念适用于Action,该操作不会返回任何结果。

Looks like the magic happens here 看起来魔术发生在这里

var builder = new ColumnBuilder(); var builder = new ColumnBuilder(); columnBuilder(builder); columnBuilder(builder);

I obviously didn't understand the fact that delegate, is just a pointer to a function (anonymous or not). 我显然不明白委托只是一个函数指针(无论是否匿名)的事实。 You still need to supply the parameter when calling it. 调用它时,您仍然需要提供参数。 (Duh!). (Du!)

All cleared up now. 现在全部清除了。

Thank you. 谢谢。

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

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