遍历实体属性,获取其值或设置其值

来源:互联网 发布:海量大数据 面试 编辑:程序博客网 时间:2024/04/27 21:43

1. 引用命名控件using System.Reflection;

2. PropertyInfo[] propertys = officeReceivefile.GetType().GetProperties();

3.遍历 PropertyInfo[] propertys

4. foreach (PropertyInfo property in propertys)

5.示例:

  foreach (PropertyInfo property in propertys)                    {                        if (property.Name.ToUpper() == "RECEIVENUMBER")                        {                            var icellTop = eachRow.CreateCell(lineCount);                            icellTop.SetCellValue(property.GetValue(officeReceivefile,null)+"");//获取内容

6.赋值:

  node=xmlDoc.GetElementsByTagName(property.Name.ToLower())[0];
                if (node != null)
                {
                    property.SetValue(siteInfo, node.InnerText, null);
                }


0 0
原创粉丝点击