Actually I would need 4 methodes. I'm using c# .NET 3.5 and windows forms.
- Get ALL control properties (also sub properites like MenuItems etc.) from current form where name matches list name //don't works ok
- Save results to XML //don't know how to save the results
- Load result from XML and
- finally set loaded control properties from XML. //work great
Now I'm doing this form step 1:
public static Dictionary<string, object> xmlDictionary;
public Control FindControlRecursive(Control container, List<string> properties)
{
foreach (Control controls in container.Controls)
{
Type controlType = controls.GetType();
PropertyInfo[] propertyInfos = controlType.GetProperties();
foreach (PropertyInfo controlProperty in propertyInfos)
{
foreach (string propertyName in properties)
{
if (controlProperty.Name == propertyName)
{
xmlDictionary.Add(controlProperty.Name, controlProperty.GetValue(controls, null));
}
}
}
Control foundCtrl = FindControlRecursive(controls, properties);
if (foundCtrl != null)
return foundCtrl;
}
return null;
}
Calling the metod:
List<string> propertyNames = new List<string>(); //list of all property names I want to save
propertyNames.Add("Name");
propertyNames.Add("Text");
propertyNames.Add("Size");
FindControlRecursive(this, propertyNames); //this is the current form
This method doesn't return all control properties and I dont know why.
Step 4.:
//field = some new field
//newValue = new value
FieldInfo controlField = form.GetType().GetField(field, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
object control = controlField.GetValue(form);
PropertyInfo property = control.GetType().GetProperty(newValue);
property.SetValue(control, items.Value, new object[0]);
Step 4 work great, but don't know how to iterate through XML reults.
Could you please help me to solve these problems.
Thank and best regards.