英語は私の母国語ではありません。私の質問が十分に明確になることを願っています。
実行時にどのアクションが 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" が割り当てられることをどのように発見できますか? まったく可能ですか?
これについては、GoogleとSOでヘルプが見つかりません。多分私は間違った方法で質問しています。
メッセージの最後には、実行/デバッグできるプログラム全体があります。
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"
}
}
}