Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
迭代特定的控件属性c#_C# - Fatal编程技术网

迭代特定的控件属性c#

迭代特定的控件属性c#,c#,C#,我需要遍历特定控件属性,并将该控件的属性名称和值保存在xml文件中。我写了几行,但有错误 private void SaveStyle() { XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; XmlWriter writer = XmlWriter.Create(Application.ExecutablePath+ @"\Products.xml", settin

我需要遍历特定控件属性,并将该控件的属性名称和值保存在xml文件中。我写了几行,但有错误

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));
    }
}
此行给出了错误
writer.WriteElementString(pi.Name,pi.GetValue(((对象)metroStyleManager1),null))

我需要做的下一个问题是,我必须从xml文件读回控件属性数据,并根据控件名称设置值。我不清楚该怎么做。所以请帮忙。谢谢

更新 我保存控件属性的完整代码&也读回

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

    }
}

确切的错误肯定会有帮助,但我想你的问题是采用两个字符串参数的方法

另一方面,返回一个对象。您必须将该
对象
转换为
字符串
。一种可能的方法是,如果
null
为空,则对其调用
.ToString()
,如果为空,则使用空字符串


需要两个字符串参数。但是
pi.GetValue()
返回类型为
object
的值。您需要将第二个参数转换为字符串:

Convert.ToString(pi.GetValue(metroStyleManager1))

这将检查对象的值是否为null,若值为null,则返回空字符串。它还将检查对象是否实现了IConvertible或IFormattable接口,并调用适当的
ToString()
方法。

什么错误?这很重要,哪种错误?格林班纳异常?请始终提供错误详细信息-类型、消息(有时甚至stacktrace也可能有用)错误与此行pi有关。GetValue(((object)metroStyleManager1),null)错误消息为无法从“object”转换为“string”。
GetValue().ToString()
?感谢您的回答,但我也想知道,当我在xml文件中保存控件属性时,如何从xml读回数据并从xml文件中初始化我不知道的控件属性值。我在谷歌上搜索,但没有找到。所以请帮帮我。thanks@Thomas你应该为此单独提出一个问题。恐怕你的第二个问题没有一个简单的通用解决方案。我正在寻找的东西都在这里完成并更新了。谢谢你的帮助。谢谢你的回答,但我也想知道当我在xml文件中保存控件属性时,如何从xml读回数据并从xml文件中初始化控件属性值,我对此一无所知。我在谷歌上搜索,但没有找到。所以请帮帮我。谢谢
writer.WriteElementString(string localName, string value)
Convert.ToString(pi.GetValue(metroStyleManager1))