На самом деле мне нужно 4 метода. Я использую c # .NET 3.5 и формы Windows.
- Получить ВСЕ свойства элемента управления (также суб-свойства, такие как MenuItems и т. Д.) Из текущей формы, где имя соответствует имени списка // не работает нормально
- Сохранить результаты в XML // не знаю, как сохранить результаты
- Загрузить результат из XML и
- наконец установить загруженные свойства элемента управления из XML. // отлично работаю
Сейчас я делаю эту форму, шаг 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;
}
Вызов метода:
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
Этот метод не возвращает все свойства элемента управления, и я не знаю, почему.
Шаг 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]);
Шаг 4 отлично работает, но не знаю, как перебирать результаты XML.
Не могли бы вы помочь мне решить эти проблемы.
Спасибо и всего наилучшего.