Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# 如何实现动作委托?_C#_Linq - Fatal编程技术网

C# 如何实现动作委托?

C# 如何实现动作委托?,c#,linq,C#,Linq,我有一节课 public class TextBoxConfig { public string Caption { get; set; } public string FieldName { get; set; } public int Width { get; set; } public string Name { get; set; } } 还有一个实用程序类,它有一个接受TextBoxConfig作

我有一节课

    public class TextBoxConfig
    {
        public string Caption { get; set; }
        public string FieldName { get; set; }
        public int Width { get; set; }
        public string Name { get; set; }
    }
还有一个实用程序类,它有一个接受TextBoxConfig作为如下参数的方法

    public class Util
    {
      public static TextBox ApplySettings(TextBoxConfig  config)
      {
         //Doing something
      }
    }
    TextBox txt = Util.ApplySettings(o =>
    {
        o.Caption = "Name";
        o.FieldName = "UserName"
        o.Width = 20;
        o.Name = "txtName";
    });              
通常,我可以像这样调用Util类ApplySettings方法

    TextBoxConfig config  = new TextBoxConfig();
    config.Caption = "Name";
    config.FieldName = "UserName"
    config.Width = 20;
    config.Name = "txtName";

    TextBox txt = Util.ApplySettings(config);
但我想像这样将参数传递给applysetting

    public class Util
    {
      public static TextBox ApplySettings(TextBoxConfig  config)
      {
         //Doing something
      }
    }
    TextBox txt = Util.ApplySettings(o =>
    {
        o.Caption = "Name";
        o.FieldName = "UserName"
        o.Width = 20;
        o.Name = "txtName";
    });              

请告诉我怎么做。

与您的愿望不完全相同,但非常接近:

TextBox txt = Util.ApplySettings(new TextBoxConfig()
{
    Caption = "Name",
    FieldName = "UserName",
    Width = 20,
    Name = "txtName"
});

注意每个设置后的逗号。请参阅。

好的,振作起来:这是同样的事情,只是用lambda表达式强制执行

TextBox txt = Util.ApplySettings(o =>
{
    o.Caption = "Name";
    o.FieldName = "UserName";
    o.Width = 20;
    o.Name = "txtName";
});

public class Util
{
    public static TextBox ApplySettings(TextBoxConfig config)
    {
        //Doing something
    }

    public static TextBox ApplySettings(Action<TextBoxConfig> modifier)
    {
        var config = new TextBoxConfig();
        modifier(config);

        return ApplySettings(config);            
    }
}
TextBox txt=Util.ApplySettings(o=>
{
o、 Caption=“Name”;
o、 FieldName=“用户名”;
o、 宽度=20;
o、 Name=“txtName”;
});
公共类Util
{
公共静态TextBox应用程序设置(TextBoxConfig配置)
{
//做某事
}
公共静态文本框应用程序设置(动作修改器)
{
var config=new TextBoxConfig();
修改器(配置);
返回应用程序设置(配置);
}
}

我不得不在语句后面加上一些分号。我更喜欢另一个答案。但我希望这能满足您对lambda表达式的需求。

谢谢您的评论,这与我正在创建的实例相同,但我想使用lambda表达式。@Neeraj Gupta,为什么?Lambda是一个函数,但您没有传递函数。若要设置TextBoxConfig类属性,正如我要求设置的那样,为什么要使用>lamda expressions<而不是简单明了地执行此操作?@Neeraj Gupta,您希望通过使用Lambda实现什么?为什么你认为这个解决方案不够好?+1:回答这个问题(不管听起来多么荒谬;p)
TextBox txt = Util.ApplySettings(o =>
{
    o.Caption = "Name";
    o.FieldName = "UserName";
    o.Width = 20;
    o.Name = "txtName";
});

public class Util
{
    public static TextBox ApplySettings(TextBoxConfig config)
    {
        //Doing something
    }

    public static TextBox ApplySettings(Action<TextBoxConfig> modifier)
    {
        var config = new TextBoxConfig();
        modifier(config);

        return ApplySettings(config);            
    }
}