在C#Action委托的用途[关闭]
题
我是用动作代表工作在C#中更多地了解他们的思路和希望,他们可能是有用的。
有没有人使用的动作代表,如果是这样,为什么?或者你可以举一些例子它可能是有用的?
解决方案
MSDN表示:
此委托所用的 Array.ForEach方法和 List.ForEach方法来执行 阵列的每个元件上的动作或 列表。
除此之外,可以使用它作为通用委托,它需要1-3参数而不返回的任何值。
其他提示
下面是示出Action委托的有用性的一个小例子
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Action<String> print = new Action<String>(Program.Print);
List<String> names = new List<String> { "andrew", "nicole" };
names.ForEach(print);
Console.Read();
}
static void Print(String s)
{
Console.WriteLine(s);
}
}
注意,在foreach方法迭代名称的集合,并执行针对集合的每个成员的print
方法。这有点模式的转变对我们C#开发人员,因为我们对编程的功能更强大的移动风格。 (有关其背后的计算机科学更多信息阅读:的http:// EN。 wikipedia.org/wiki/Map_(higher-order_function)。
现在如果你使用C#3可以光滑这个了位,象这样的λ表达式:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
List<String> names = new List<String> { "andrew", "nicole" };
names.ForEach(s => Console.WriteLine(s));
Console.Read();
}
}
好了一两件事你可以做的是,如果你有一个开关:
switch(SomeEnum)
{
case SomeEnum.One:
DoThings(someUser);
break;
case SomeEnum.Two:
DoSomethingElse(someUser);
break;
}
和与行动的力量的权力,你可以把该交换机成词典:
Dictionary<SomeEnum, Action<User>> methodList =
new Dictionary<SomeEnum, Action<User>>()
methodList.Add(SomeEnum.One, DoSomething);
methodList.Add(SomeEnum.Two, DoSomethingElse);
...
methodList[SomeEnum](someUser);
或者你也可以借此更远:
SomeOtherMethod(Action<User> someMethodToUse, User someUser)
{
someMethodToUse(someUser);
}
...
var neededMethod = methodList[SomeEnum];
SomeOtherMethod(neededMethod, someUser);
只是几个例子。当然更明显的用途是LINQ的扩展方法。
您可以使用行动短事件处理程序:
btnSubmit.Click += (sender, e) => MessageBox.Show("You clicked save!");
我使用的动作代表这样一个项目一次:
private static Dictionary<Type, Action<Control>> controldefaults = new Dictionary<Type, Action<Control>>() {
{typeof(TextBox), c => ((TextBox)c).Clear()},
{typeof(CheckBox), c => ((CheckBox)c).Checked = false},
{typeof(ListBox), c => ((ListBox)c).Items.Clear()},
{typeof(RadioButton), c => ((RadioButton)c).Checked = false},
{typeof(GroupBox), c => ((GroupBox)c).Controls.ClearControls()},
{typeof(Panel), c => ((Panel)c).Controls.ClearControls()}
};
其中所有它的作用是存储一个动作(方法调用)针对类型的控制,以便可以清除所有控件的窗体上回有默认值。
有关的操作<>如何使用的例子。
Console.WriteLine具有签名satisifies Action<string>
。
static void Main(string[] args)
{
string[] words = "This is as easy as it looks".Split(' ');
// Passing WriteLine as the action
Array.ForEach(words, Console.WriteLine);
}
希望这有助于
我用它当我处理非法跨线程调用,例如:
DataRow dr = GetRow();
this.Invoke(new Action(() => {
txtFname.Text = dr["Fname"].ToString();
txtLname.Text = dr["Lname"].ToString();
txtMI.Text = dr["MI"].ToString();
txtSSN.Text = dr["SSN"].ToString();
txtSSN.ButtonsRight["OpenDialog"].Visible = true;
txtSSN.ButtonsRight["ListSSN"].Visible = true;
txtSSN.Focus();
}));
我必须里德·科普塞SO用户65358的溶液给予信用。我的答案全的问题是 SO问题2587930个
我用它作为在事件处理程序的回调。当我提出时,我通过采取一个字符串参数的方法。这是该事件的提高是这样的:
SpecialRequest(this,
new BalieEventArgs
{
Message = "A Message",
Action = UpdateMethod,
Data = someDataObject
});
在方法:
public void UpdateMethod(string SpecialCode){ }
在是事件参数的类声明:
public class MyEventArgs : EventArgs
{
public string Message;
public object Data;
public Action<String> Action;
}
此方式我可以调用从事件处理程序通过用某些参数来更新数据的方法。我使用它来向用户请求的某些信息。
我们使用的测试很多Action委托功能。当我们需要建立一些默认的对象,以后需要修改它。我做了小例子。要建立缺省人(李四)对象,我们使用BuildPerson()
功能。后来我们加李四太多,但我们修改了她的生日和名字和高度。
public class Program
{
public static void Main(string[] args)
{
var person1 = BuildPerson();
Console.WriteLine(person1.Firstname);
Console.WriteLine(person1.Lastname);
Console.WriteLine(person1.BirthDate);
Console.WriteLine(person1.Height);
var person2 = BuildPerson(p =>
{
p.Firstname = "Jane";
p.BirthDate = DateTime.Today;
p.Height = 1.76;
});
Console.WriteLine(person2.Firstname);
Console.WriteLine(person2.Lastname);
Console.WriteLine(person2.BirthDate);
Console.WriteLine(person2.Height);
Console.Read();
}
public static Person BuildPerson(Action<Person> overrideAction = null)
{
var person = new Person()
{
Firstname = "John",
Lastname = "Doe",
BirthDate = new DateTime(2012, 2, 2)
};
if (overrideAction != null)
overrideAction(person);
return person;
}
}
public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public DateTime BirthDate { get; set; }
public double Height { get; set; }
}