dha*_*ech 6 c# dart method-cascades
Dart编程语言支持方法级联.方法级联将允许以下Silverlight/WPF C#代码:
var listBox = new ListBox();
listBox.Width = 200;
listBox.MouseEnter += (s, e) => Console.WriteLine("MouseEnter");
var button1 = new Button() { Content = "abc" };
button1.Click += (s, e) => Console.WriteLine("button1.Click");
listBox.Items.Add(button1);
var button2 = new Button() { Content = "def" };
button2.Click += (s, e) => Console.WriteLine("button2.Click");
listBox.Items.Add(button2);
ContentPanel.Children.Add(listBox);
Run Code Online (Sandbox Code Playgroud)
改为:
ContentPanel.Children.Add(
new ListBox()
..Width = 200
..MouseEnter += ((s, e) => Console.WriteLine("MouseEnter"))
..Items.Add(
new Button()
..Content = "abc";
..Click += ((s, e) => Console.WriteLine("button 1 Click")))
..Items.Add(
new Button()
..Content = "def";
..Click += (s, e) => (Console.WriteLine("button 2 Click"))));
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有办法模拟或近似接近C#中的方法级联?
这是我提出的一种方法.鉴于此扩展方法:
public static T Call<T>(this T obj, Action<T> proc)
{
proc(obj);
return obj;
}
Run Code Online (Sandbox Code Playgroud)
上面的例子可以写成如下:
ContentPanel.Children.Add(
new ListBox().Call(o => {
o.Width = 200;
o.MouseEnter += (s, e) => Console.WriteLine("MouseEnter");
o.Items.Add(
new Button().Call(b => {
b.Content = "abc";
b.Click += (s, e) => Console.WriteLine("button 1 Click"); }));
o.Items.Add(
new Button().Call(b => {
b.Content = "def";
b.Click += (s, e) => Console.WriteLine("button 2 Click"); })); }));
Run Code Online (Sandbox Code Playgroud)
我不认为这很漂亮.:-)但它确实能够应用流畅的风格.
我认为您可以通过使用流畅的界面来接近您想要实现的目标。它将允许您链接方法以在一条语句中创建和初始化对象。
你可以得到类似的东西:
Fluent fluent = new Fluent();
var panel = fluent.CreateControlPanel().Children()
.AddListBox().SetWidth(200).AddMouseEnterEvent((s, e) => { }).Create()
.AddTextBox().SetText("Foo").Create()
.GetControlPanel();
Run Code Online (Sandbox Code Playgroud)
这个想法是一个方法返回一个对象,允许初始化另一个对象。初始化器链可以在任何项上调用“终结器”方法(上图Create
),该方法返回原始对象(上图Children
)以继续添加其他对象或配置初始对象。
例如, inAddListBox
返回一个类型的对象ListBoxSetup
,该对象具有一堆类似SetWidth
or 的方法AddMouseEnterEvent
。在这种情况下,Children
也将是一个特殊的对象(例如类型ChildSetup
),它具有一堆方法,例如AddListBox
或AddTextBox
。每个方法都负责创建一个类型的对象ListBox
或TextBox
设置要创建的底层对象的属性。Fluent
将有一个方法可以正确设置返回整个对象结构。
看一下这个链接: http://blog.raffaeu.com/archive/2010/06/26/how-to-write- Fluent-interface-with-c-and-lambda.aspx
下面是为实现上述内容而创建的底层代码的示例。当然,代码的架构可以大大改进,但这里只是为了示例。
public class Fluent
{
public ControlPanelCreator CreateControlPanel()
{
return new ControlPanelCreator(new StackPanel(), this);
}
}
public class ControlPanelCreator
{
#region Fields
private Fluent fluent;
private Panel panel;
#endregion
#region Constructors
internal ControlPanelCreator(Panel panel, Fluent fluent)
{
this.fluent = fluent;
this.panel = panel;
}
#endregion
#region Methods
public ControlPanelChildrenCreator Children()
{
return new ControlPanelChildrenCreator(this.panel, this);
}
#endregion
}
public class ControlPanelChildrenCreator
{
#region Fields
private ControlPanelCreator panelCreator;
private Panel panel;
#endregion
#region Constructors
internal ControlPanelChildrenCreator(Panel panel, ControlPanelCreator panelCreator)
{
this.panel = panel;
this.panelCreator = panelCreator;
}
#endregion
#region Methods
public ListBoxCreator AddListBox()
{
ListBox listBox = new ListBox();
this.panel.Children.Add(listBox);
return new ListBoxCreator(listBox, this);
}
public TextBoxCreator AddTextBox()
{
TextBox textBox = new TextBox();
this.panel.Children.Add(textBox);
return new TextBoxCreator(textBox, this);
}
public Panel GetControlPanel()
{
return this.panel;
}
#endregion
}
public class ListBoxCreator
{
#region Fields
private ListBox listbox;
private ControlPanelChildrenCreator parentCreator;
#endregion
#region Constructors
internal ListBoxCreator(ListBox listBox, ControlPanelChildrenCreator parentCreator)
{
this.listbox = listBox;
this.parentCreator = parentCreator;
}
#endregion
#region Methods
public ListBoxCreator SetWidth(int width)
{
this.listbox.Width = width;
return this;
}
public ListBoxCreator AddMouseEnterEvent(Action<object, MouseEventArgs> action)
{
this.listbox.MouseEnter += new MouseEventHandler(action);
return this;
}
public ControlPanelChildrenCreator Create()
{
return this.parentCreator;
}
#endregion
}
public class TextBoxCreator
{
#region Fields
private TextBox textBox;
private ControlPanelChildrenCreator parentCreator;
#endregion
#region Constructors
internal TextBoxCreator(TextBox textBox, ControlPanelChildrenCreator parentCreator)
{
this.textBox = textBox;
this.parentCreator = parentCreator;
}
#endregion
#region Methods
public TextBoxCreator SetText(string defaultText)
{
this.textBox.Text = defaultText;
return this;
}
public ControlPanelChildrenCreator Create()
{
return this.parentCreator;
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1223 次 |
最近记录: |