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.

Sir Crusher
  • 1,548
  • 14
  • 28

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
  • 69,953
  • 7
  • 104
  • 182
  • How would I get the values of theses properties? itemDetails[2] = equipmentProperties.First(ep => ep.Name == "Manufacturer").GetValue(equipmentField, null).ToString(); Doesn't work. – Sir Crusher Jul 22 '14 at 11:39
  • You need to call [propertyInfo.GetValue method](http://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection-in-c-sharp) – Sriram Sakthivel Jul 22 '14 at 11:41
  • @afm Doesn't work is not helpful, post a new question saying what happens when you try that. You may get quick answers.. – Sriram Sakthivel Jul 22 '14 at 11:42
  • Will do, have to wait 90 minutes though :/ – Sir Crusher Jul 22 '14 at 11:44