fieldInfo.setValue():类型的对象' system.string'无法转换为type' system.int32'
程序逻辑:
- 在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 theDefaultAttributes
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论