2013-03-30 47 views
0

我有一个类如何实现Action委托?

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 
     } 
    } 

一般来说,我可以调用的Util类ApplySettings方法这样

的参数的方法
TextBoxConfig config = new TextBoxConfig(); 
    config.Caption = "Name"; 
    config.FieldName = "UserName" 
    config.Width = 20; 
    config.Name = "txtName"; 

    TextBox txt = Util.ApplySettings(config); 

但我想将参数传递给ApplySettings像这样

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

请建议我我该怎么办呢..

回答

0

好吧,自己动手:这里是一样的东西,只是用lambda表达式强制执行。我不得不在语句后添加一些分号。我更喜欢其他答案。但我希望这能满足你对lambda表达式的渴望。

+0

+1:回答这个问题(不管它听起来多么荒谬; p) – leppie

+0

很好......谢谢Das :) –

0

不完全一样,你的愿望,但八九不离十:

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

注意每次设置后的逗号。请参阅http://msdn.microsoft.com/en-us/library/vstudio/bb397680.aspx

+0

感谢您的评论这与我创建实例一样,但我想使用Lambda表达式。 –

+0

@Neeraj Gupta,为了什么? Lambda是一个函数,但你没有传递函数。 – aush

+0

设置TextBoxConfig类的属性,因为我要求设置 –