简体   繁体   English

遍历特定的控件属性c#

[英]Iterate through a specific controls property c#

i need to iterate through a specific controls property and save the that control's property name & value in xml file. 我需要遍历特定的控件属性并将该控件的属性名称和值保存在xml文件中。 i wrote few line but getting error. 我写了几行,但报错。

private void SaveStyle()
{
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    XmlWriter writer = XmlWriter.Create(Application.ExecutablePath+ @"\Products.xml", settings);

    PropertyInfo[] properties = metroStyleManager1.GetType().GetProperties();
    writer.WriteStartDocument();
    foreach (PropertyInfo pi in properties)
    {
        writer.WriteElementString(pi.Name,pi.GetValue(((object)metroStyleManager1),null));
    }
}

this line is giving error writer.WriteElementString(pi.Name,pi.GetValue(((object)metroStyleManager1),null)); 这行给错误writer.WriteElementString(pi.Name,pi.GetValue(((object)metroStyleManager1),null));

next issue which i need to do that i have to read back the controls property data from xml file and set the value as per the controls name. 我需要做的下一个问题是我必须从xml文件中读取控件属性数据,并按照控件名称设置值。 which is not clear to me that how to do it. 我不清楚该怎么做。 so please help. 所以请帮忙 thanks 谢谢

UPDATE UPDATE

my full code to save controls property & read back too. 我完整的代码来保存控件属性并回读。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml;
using MetroFramework;
namespace CSRAssistant
{
    class Utils
    {
        public static void SaveProperty(System.ComponentModel.Component _Control)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\Products.xml", settings);

            PropertyInfo[] properties = _Control.GetType().GetProperties();
            writer.WriteStartElement("metroStyleManager");
            foreach (PropertyInfo pi in properties)
            {
                writer.WriteElementString(pi.Name, Convert.ToString(pi.GetValue(_Control, null)));
            }
            writer.WriteEndDocument();

            writer.Flush();
            writer.Close();
        }

       public static void ReadProperty(System.ComponentModel.Component _Control)
    {
        string _property = "", _value = "";
        if (System.IO.File.Exists(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\Products.xml"))
        {
            XmlReader rdr = XmlReader.Create(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\Products.xml");
            while (rdr.Read())
            {
                if (rdr.NodeType == XmlNodeType.Element)
                {
                    if (rdr.LocalName.ToUpper() != "METROSTYLEMANAGER")
                    {
                        _property = rdr.LocalName;
                        _value = rdr.ReadInnerXml();
                        if (_property.ToUpper() == "STYLE")
                            ((MetroFramework.Components.MetroStyleManager)_Control).Style = (MetroColorStyle)Enum.Parse(typeof(MetroColorStyle), _value);
                        if (_property.ToUpper() == "THEME")
                            ((MetroFramework.Components.MetroStyleManager)_Control).Theme = (MetroThemeStyle)Enum.Parse(typeof(MetroThemeStyle), _value);
                        //else
                        //    _Control.GetType().GetProperty(_property).SetValue(_Control, _value, null);
                    }
                }
            }

            rdr.Close();
        }
    }

    }
}
writer.WriteElementString(string localName, string value)

expects two string arguments. 需要两个字符串参数。 But pi.GetValue() returns value of type object . 但是pi.GetValue()返回object类型的值。 You need to convert second parameter to string: 您需要将第二个参数转换为字符串:

Convert.ToString(pi.GetValue(metroStyleManager1))

That will check if value of object is not null, and return empty string if value is null. 这将检查object的值是否不为null,如果value为null则返回空字符串。 It also will check if object implements IConvertible or IFormattable interfaces and call appropriate ToString() method. 它还将检查对象是否实现IConvertible或IFormattable接口,并调用适当的ToString()方法。

The exact error would surely help, but I guess your problem is the WriteElementString method which takes two string parameters. 确切的错误肯定会有所帮助,但是我想您的问题是使用两个字符串参数的WriteElementString方法。

PropertyInfo.GetValue on the other hand returns an object. 另一方面, PropertyInfo.GetValue返回一个对象。 You have to convert that object to a string . 您必须将该object转换为string A possible way would be to call .ToString() on it if it's not null and use an empty string if it is. 一种可能的方法是在不为null情况下对其调用.ToString() ,如果为null使用一个空字符串。


foreach (PropertyInfo pi in properties)
{
    object obj = pi.GetValue(metroStyleManager1, null);                      
    writer.WriteElementString(pi.Name, obj != null ? obj.ToString() : String.Empty);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM