首先,我是新手。我已经创建了class:
using System.Reflection; public class EmployeeInfo { public string EmployeeName { get; set; } public string PhoneNumber { get; set; } public string Office { get; set; } public string Department { get; set; } public string Position { get; set; } public string PhoneType { get; set; } public bool IsPublic { get; set; } }
现在,我正在尝试开发一种方法,该方法将通过反射使用一些业务逻辑(如果为null,然后为空字符串等)填充所有属性,并返回的列表EmployeeInfo
。我认为它应该看起来像这样:
public ListGetEmployeeInfo(SPListItemCollection splic) { var listEmployeeInfo = new List (); var propertyNames = new List () {"EmployeeName","Position","Office","IsPublic"}; foreach (SPListItem item in splic) { var employeeInfo = new Models.EmployeeInfo(); foreach (var propertyName in propertyNames) { string newData = ""; if (item[propertyName] != null) { newData = item[propertyName].ToString(); } employeeInfo.GetProperty(propertyName).SetValue(employeeInfo, newData, null); } listEmployeeInfo.Add(employeeInfo); } return listEmployeeInfo; }
但是我不能在这一行调用GetProperty
或SetValue
扩展方法:
employeeInfo.GetProperty(propertyName).SetValue(employeeInfo, newData, null);
错误消息说我的Models.EmployeeInfo
类不包含定义GetProperty
和扩展方法GetProperty
。缺什么 ?谢谢 。
GetProperty是Type类上的方法。
employeeInfo.GetType().GetProperty(propertyName).SetValue(employeeInfo, newData, null);