fieldInfo.setValue():类型的对象' system.string'无法转换为type' system.int32'

发布于 2025-01-29 16:55:34 字数 2494 浏览 3 评论 0原文

程序逻辑:

  • 在C#程序中为雇员定义一些默认值
  • ,读取有关不同员工的XML文件
  • 使用反思浏览defaultAttributes的所有字段< /code>类,
  • 如果字段名称作为员工的XML节点中的属性存在,则用属性值

雇员defaultAttributes class

public class DefaultAttributes
{
    public string EmployeeName = "XYZ";
    public int[] ProjectCodes = new int[] { 1, 1 };
    public string city = "Munich";
}

xml file:

   <TopNode Id="A">
      <MiddleNode EmployeeName="John">
         <InnerMostNode Age="46" Average="88.15" ProjectCodes="46 112" City="Berlin">            
         </InnerMostNode>
      </MiddleNode>
      <MiddleNode EmployeeName="Claudia">
         <InnerMostNode Age="50" Average="96.58" ProjectCodes="9 510">            
         </InnerMostNode>
      </MiddleNode>
   </TopNode>

使用反射来覆盖默认值:

    private void OverrideAttribites(XmlNode xmlNode, DefaultAttributes nodeAttributes)
    {
        //Traverse through all fileds of DefaultAttributes class
        FieldInfo[] fieldsOfDefaultAttributesclass = typeof(DefaultAttributes).GetFields(BindingFlags.Public | BindingFlags.Instance);
        foreach (var field in fieldsOfDefaultAttributesclass)
        {
            // Check if the "field" is present in the attributes of the current XML node
            string fieldName = field.Name;
            if (xmlNode.Attributes[fieldName] is not null) //i.e. field is present in the Attributes list
            {
                var attValue = xmlNode.Attributes[fieldName].Value;

                if (attValue.Contains(' ')) //i.e. attribute value should be stored in an array type
                {
                    if (!fieldName.Contains('['))
                    {
                        throw new Exception("Field type is not an array type but the XML attribute value contains an array");
                    }

                    //Set Array Value
                }
                else
                {
                    //PROBLEM: THIS LINE THROWS EXCEPTION !!!!!
                    field.SetValue(nodeAttributes, xmlNode.Attributes[fieldName].Value);
                }
            }
        }
    }

问题:我的XML文件中的属性值String格式。我如何使用属性的值以通用方式设置字段值(字段可以是int, double ,,int [ ]等)?

Program Logic:

  • Define some default values for an Employee in the C# program
  • Read an XML file about different Employees
  • Use Reflections to go through all the fields of the DefaultAttributes class
  • If a field name is present as an attribute in an Employee's XML node then, override the default value with the attribute value

Employee DefaultAttributes Class

public class DefaultAttributes
{
    public string EmployeeName = "XYZ";
    public int[] ProjectCodes = new int[] { 1, 1 };
    public string city = "Munich";
}

XML File:

   <TopNode Id="A">
      <MiddleNode EmployeeName="John">
         <InnerMostNode Age="46" Average="88.15" ProjectCodes="46 112" City="Berlin">            
         </InnerMostNode>
      </MiddleNode>
      <MiddleNode EmployeeName="Claudia">
         <InnerMostNode Age="50" Average="96.58" ProjectCodes="9 510">            
         </InnerMostNode>
      </MiddleNode>
   </TopNode>

Use of Reflections To Override Default Values:

    private void OverrideAttribites(XmlNode xmlNode, DefaultAttributes nodeAttributes)
    {
        //Traverse through all fileds of DefaultAttributes class
        FieldInfo[] fieldsOfDefaultAttributesclass = typeof(DefaultAttributes).GetFields(BindingFlags.Public | BindingFlags.Instance);
        foreach (var field in fieldsOfDefaultAttributesclass)
        {
            // Check if the "field" is present in the attributes of the current XML node
            string fieldName = field.Name;
            if (xmlNode.Attributes[fieldName] is not null) //i.e. field is present in the Attributes list
            {
                var attValue = xmlNode.Attributes[fieldName].Value;

                if (attValue.Contains(' ')) //i.e. attribute value should be stored in an array type
                {
                    if (!fieldName.Contains('['))
                    {
                        throw new Exception("Field type is not an array type but the XML attribute value contains an array");
                    }

                    //Set Array Value
                }
                else
                {
                    //PROBLEM: THIS LINE THROWS EXCEPTION !!!!!
                    field.SetValue(nodeAttributes, xmlNode.Attributes[fieldName].Value);
                }
            }
        }
    }

Question: The attributes values in my XML file are present string format. How do I use the attribute's value to set a field's value in a generic way (field could be int, double, int[] etc.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文