Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Lambda和action委托的使用_C#_Lambda - Fatal编程技术网

C# Lambda和action委托的使用

C# Lambda和action委托的使用,c#,lambda,C#,Lambda,我得到了几行代码,其中configure方法只调用并传递字符串值,而ConfigureWith函数expect delegate。有人能帮我理解ConfigureWith()方法是如何工作的吗。谢谢 MailTemplate .ConfigureWith(mt => mt.MailBody = "hello world") .ConfigureWith(mt => mt.MailFrom = "rdingwall@gmail.com") .DoSomeOtherStuff() .Bu

我得到了几行代码,其中configure方法只调用并传递字符串值,而ConfigureWith函数expect delegate。有人能帮我理解ConfigureWith()方法是如何工作的吗。谢谢

MailTemplate
.ConfigureWith(mt => mt.MailBody = "hello world")
.ConfigureWith(mt => mt.MailFrom = "rdingwall@gmail.com")
.DoSomeOtherStuff()
.Build();
这方面的实施将是:

public class MailTemplate
{
// regular auto properties
public string MailFrom { get; set; }
public string MailBody { get; set; }

public MailTemplate ConfigureWith(Action<MailTemplate> func)
{
    func(this);
    return this;
}


}
公共类邮件模板
{
//常规自动属性
公共字符串MailFrom{get;set;}
公共字符串邮件体{get;set;}
公用邮件模板配置(操作函数)
{
func(本);
归还这个;
}
}

正如所写,它似乎完全没有意义,您也可以直接在
邮件模板上设置属性

通常,在像这样的fluent builder中,您会保存每个
ConfigureWith
调用传递的操作,然后在以后执行它们

如果你能更详细地解释一下你希望用你正在创建的流畅语法实现什么,这可能会有所帮助。在编写时,它也不会编译,因为第一次调用需要一个静态方法。你能展示真实的代码吗


您可能还想查看StackOverflow中有关fluent方法的其他答案(例如)

我看到上面的示例来自您之前询问的问题的答案。我仍然不能完全确定你想做什么,正如伊恩·默瑟所说的,这是毫无意义的。但是,如果您只是想了解它的功能,那么让我们首先来看一个工作示例:

using System;

namespace ScratchApp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var mailTemplate = BuildMailTemplate(
                mt => mt.MailBody = "hello world", 
                mt => mt.MailFrom = "rdingwall@gmail.com");
        }

        private static MailTemplate BuildMailTemplate(
            Action<MailTemplate> configAction1, 
            Action<MailTemplate> configAction2)
        {
            var mailTemplate = new MailTemplate();
            mailTemplate.ConfigureWith(configAction1)
                .ConfigureWith(configAction2)
                .DoSomeOtherStuff()
                .Build();
            return mailTemplate;
        }
    }

    public class MailTemplate
    {
        public string MailFrom { get; set; }
        public string MailBody { get; set; }

        public MailTemplate DoSomeOtherStuff()
        {
            // Do something
            return this;
        }

        public MailTemplate Build()
        {
            // Build something
            return this;
        }

        public MailTemplate ConfigureWith(Action<MailTemplate> func)
        {
            func(this);
            return this;
        }
    }
}
使用系统;
命名空间ScratchApp
{
内部课程计划
{
私有静态void Main(字符串[]args)
{
var mailTemplate=BuildMailTemplate(
mt=>mt.MailBody=“你好,世界”,
mt=>mt.MailFrom=”rdingwall@gmail.com");
}
私有静态邮件模板BuildMailTemplate(
行动1,
行动2)
{
var mailTemplate=新的mailTemplate();
mailTemplate.ConfigureWith(configAction1)
.ConfigureWith(configAction2)
.DoSomeOtherStuff()
.Build();
返回邮件模板;
}
}
公共类邮件模板
{
公共字符串MailFrom{get;set;}
公共字符串邮件体{get;set;}
公共邮件模板DoSomeOtherStuff()
{
//做点什么
归还这个;
}
公共邮件模板生成()
{
//造东西
归还这个;
}
公用邮件模板配置(操作函数)
{
func(本);
归还这个;
}
}
}
这和以前一样毫无意义,但它会构建。调用.ConfigureWith()时发生的情况是,不是传递一个普通值,而是传递一个函数。在上面的示例中,我实际上将函数声明为传递到BuildMailTemplate()方法中的参数,然后在构建和配置模板时执行这些参数。您可以通过逐行(例如,VisualStudio中的F11)遍历代码,在lambda表达式本身中设置断点,然后查看调用堆栈来了解它的工作原理


如果您对lambdas的语法感到困惑——当您第一次习惯它时,箭头语法确实有点复杂——那么请随意查看它的语法,或者只需谷歌“c#lambdas”就可以了。

这在现实世界中的用途是什么?流畅的界面是真正的需求吗?(从上面的例子来看,这似乎是因为没有太多真正的原因而增加了复杂性。)我只是不明白为什么人们只喜欢给否定的分数而不加任何分数。我对fluent不感兴趣,而是想知道当字符串数据传递时ConfigureWith()函数是如何工作的,比如“hello world”,你可以解释一下。谢谢