英语不是我的母语,我希望我能让我的问题足够清楚。
我想知道在执行时如何识别 Action 类中包含哪些操作(准确地说,将调用哪些属性)。
想象一个简单的 Employee 类......
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
...以及一个以 Action<> 作为参数的简单 Update() 方法
public static void Update(Action<Employee> action) {
//HERE : how could i know which properties will be assigned by analyzing the Action<Employee> object ?
Employee em = new Employee();
action(em);
//Only FirstName and LastName have been assigned to "em"
}
Update 方法可以这样调用:
//Call the Update method with only two "actions" : assign FirstName and LastName properties.
Update(e => { e.FirstName = "firstname"; e.LastName = "lastname"; });
我的问题是在 Update() 方法中确定哪些属性已“计划”用于 Action<> 对象中的赋值(以及相关值)。
通过分析 Action<> 对象,我如何发现只有属性 FirstName 和 LastName 将被分配值 "firstname" 和 "lastname" ?有可能吗?
我在谷歌和 SO 上找不到任何帮助。也许我以错误的方式问这个问题。
消息的末尾是可以执行/调试的整个程序。
在此先感谢这里的所有人。
麦克风
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestAction
{
class Program
{
public class Employee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Email { get; set; }
}
static void Main(string[] args)
{
//Call the Update method with only two "actions" : assign FirstName and LastName properties.
Update(e => { e.FirstName = "firstname"; e.LastName = "lastname"; });
}
public static void Update(Action<Employee> action)
{
//HERE : how could i know which properties will be assigned by analyzing the Action<Employee> object ?
Employee em = new Employee();
action(em);
//Only FirstName and LastName have been assigned to "em"
}
}
}