从实例化的表单中检索FieldInfo对象的属性

时间:2014-07-22 10:37:29

标签: c#

我正在尝试从动态实例化的表单中获取来自公共DTO对象的属性。

Form equipmentDialog = (Form)Activator.CreateInstance(_lookups.First(d => d.Key == lvwEquipmentCategories.FocusedItem.Text).Dialog, (object)null);

if (equipmentDialog.ShowDialog() == DialogResult.OK)
{
    FieldInfo equipmentField = equipmentDialog.GetType().GetFields().First(); //Equipment (DtoEquipment) - there is only one.
    List<PropertyInfo> equipmentProperties = equipmentField.GetType().GetProperties().ToList();
}

这并没有给我我想要的属性。它只是给了我一堆像IsPublic,IsPrivate等属性。

我正在寻找的结果是这样的:

DtoEquipment test = new DtoEquipment();
List<PropertyInfo> testProperties = test.GetType().GetProperties().ToList();

这为我提供了DTO对象的属性。 但我显然需要在实例化的表单上从DTO对象中获取这些属性。

我尝试将FieldInfo作为DTO进行投射,但这不起作用。

1 个答案:

答案 0 :(得分:0)

这应该有效。使用FieldInfo.FieldType获取字段的类型。

if (equipmentDialog.ShowDialog() == DialogResult.OK)
{
    FieldInfo equipmentField = equipmentDialog.GetType().GetFields().First();
    List<PropertyInfo> equipmentProperties = equipmentField.FieldType.GetProperties().ToList();
}