C# 动作委托在C语言中的应用#
我与C#的行动代表们一起工作,希望能更多地了解他们,并思考他们在哪里可能有用 是否有人使用过Action Delegate,如果有,原因是什么?或者你能举一些可能有用的例子吗?MSDN说: 此委托由 Array.ForEach方法和 List.ForEach方法来执行 对数组或数组的每个元素执行的操作 名单C# 动作委托在C语言中的应用#,c#,lambda,delegates,action,C#,Lambda,Delegates,Action,我与C#的行动代表们一起工作,希望能更多地了解他们,并思考他们在哪里可能有用 是否有人使用过Action Delegate,如果有,原因是什么?或者你能举一些可能有用的例子吗?MSDN说: 此委托由 Array.ForEach方法和 List.ForEach方法来执行 对数组或数组的每个元素执行的操作 名单 除此之外,您可以将其用作一般委托,它接受1-3个参数而不返回任何值。我将其用作事件处理程序中的回调。当我引发事件时,我传入一个以字符串作为参数的方法。这就是此次活动的举办情况: Specia
除此之外,您可以将其用作一般委托,它接受1-3个参数而不返回任何值。我将其用作事件处理程序中的回调。当我引发事件时,我传入一个以字符串作为参数的方法。这就是此次活动的举办情况:
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;
}
公共类MyEventArgs:EventArgs
{
公共字符串消息;
公共对象数据;
公共行动;
}
通过这种方式,我可以使用某个参数调用从事件处理程序传递的方法来更新数据。我使用它向用户请求一些信息。我曾经在项目中使用过这样的操作委托:
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()}
};
private static Dictionary controldefaults=new Dictionary(){
{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()}
};
它所做的就是针对某一类型的控件存储一个操作(方法调用),这样您就可以清除表单上的所有控件,使其返回默认值。您可以做的一件事是,如果您有一个开关:
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);
或者你可以更进一步:
SomeOtherMethod(Action<User> someMethodToUse, User someUser)
{
someMethodToUse(someUser);
}
只是几个例子。当然,更明显的用法是Linq扩展方法。有关如何使用动作的示例 Console.WriteLine具有满足
操作要求的签名
希望这有帮助您可以对短事件处理程序使用操作:
btnSubmit.Click += (sender, e) => MessageBox.Show("You clicked save!");
下面是一个小示例,它显示了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);
}
}
我在处理非法跨线程调用时使用它,例如:
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();
}));
我必须感谢Reed Copsey SO用户65358提供的解决方案。我的完整问题和答案是我们在测试中使用了很多动作委托功能。当我们需要构建一些默认对象,然后需要修改它时。我没有做出什么榜样。为了构建默认的person(johndoe)对象,我们使用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; }
}
公共类程序
{
公共静态void Main(字符串[]args)
{
var person1=BuildPerson();
Console.WriteLine(person1.Firstname);
Console.WriteLine(person1.Lastname);
Console.WriteLine(person1.生日);
控制台。书写线(人员1。高度);
var person2=BuildPerson(p=>
{
p、 Firstname=“简”;
p、 生日=日期时间。今天;
p、 高度=1.76;
});
Console.WriteLine(person2.Firstname);
Console.WriteLine(person2.Lastname);
控制台。书写线(人2。出生日期);
控制台。书写线(人员2。高度);
Console.Read();
}
publicstaticpersonbuildperson(actionoverrideaction=null)
{
var person=新人员()
{
Firstname=“约翰”,
Lastname=“Doe”,
生日=新的日期时间(2012年2月2日)
};
if(覆盖动作!=null)
覆盖(人);
返回人;
}
}
公共阶层人士
{
公共字符串名{get;set;}
公共字符串Lastname{get;set;}
公共日期时间出生日期{get;set;}
公共双倍高度{get;set;}
}
我从未注意到这些多参数版本的操作。谢谢。很好,没有太大的变化,但是有一种叫做keyedbyTypeCollection的东西,尽管我认为它围绕着词汇(类型,对象)可能是。很好,我认为这可以用作决策表。很好-这是一种重构模式“用多态替换条件”。你好,Sorskoot,你能介绍一下UpdateMethod、MyEventArgs和新的BalieEventArgs是如何一起玩的吗。字符串消息是否传递到UpdateMethod:UpdateMethod(“消息”)中?哪个方法使用对象“someDataObject”?提前感谢你也可以用它们来做长的;btnSubmit.Click+=(发件人,e)=>{MessageBox.Show(“您单击了保存!”);MessageBox.Show(“您真的这么做了!”);};
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);
}
}
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();
}
}
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();
}));
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; }
}