我正在创建一个程序,用户正在创建一个操作列表。可能的操作都有多个需要指定的属性。我想在列表框中显示这些属性,类似于 Visual Studio 在设计模式下显示对象属性的方式。字符串获取文本框,布尔值获取复选框等。我已经弄清楚如何在列表中显示对象的成员,但我正在努力弄清楚如何为每个控件创建回调。基本结构如下:
void createPropertyList(object move)
{
Type type = move.GetType();
FieldInfo[] fields = type.GetFields();
propertyListBox.Items.Clear();
foreach (var field in fields)
{
string name = field.Name;
object temp = field.GetValue(move);
if (temp is double)
{
double value = (double)temp;
Label l = new Label();
l.Content = name;
TextBox t = new TextBox();
t.Text = value.ToString("0.0000");
// put it in a grid, format it, blah blah blah
propertyListBox.Items.Add(grid);
}
// reflect on other types
}
}
我假设将涉及一个 lambda,但我如何恢复 FieldInfo 数组以实际引用这些成员,以便我可以将用户的输入放回对象中?