0

I am trying to get the properties from a public DTO object that comes from a dynamically instantiated form.

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();
}

This doesn't give me the properties I am looking for. It just gives me a bunch of properties like IsPublic, IsPrivate etc.

The result i'm looking for is something like this:

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

This gives me the properties of my DTO object. But I obviously need to get these properties from the DTO object on the instantiated form.

I have tried casting the FieldInfo as the DTO but that doesn't work.

Sean Thorburn
  • 1,728
  • 17
  • 31

1 Answers1

0

This should work. Use FieldInfo.FieldType to get the type of the field.

if (equipmentDialog.ShowDialog() == DialogResult.OK)
{
    FieldInfo equipmentField = equipmentDialog.GetType().GetFields().First();
    List<PropertyInfo> equipmentProperties = equipmentField.FieldType.GetProperties().ToList();
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189