c# - 使用 foreach 迭代时跳过类的一些属性
问题描述
我想在遍历 Student 类时跳过 ID 属性,但是对于某些周期,该属性必须是可浏览的
internal class Student
{
[DisplayName("N")]
public int ID { get; set; }
[DisplayName("Name")]
public string FirstName { get; set; }
[DisplayName("Surname")]
public string LastName { get; set; }
[DisplayName("ID Number")]
public string IDNumber { get; set; }
[DisplayName("Mobile Number")]
public string Mobile { get; set; }
[DisplayName("Class")]
public byte Grade { get; set; }
[Browsable(false)]
public int StatusID { get; set; }
}
这是遍历学生类属性的代码
int num = 1;
PropertyInfo[] properties = typeof(Student).GetProperties();
foreach (PropertyInfo property in properties)
{
property.SetValue(newStudent, Convert.ChangeType(_textBoxes[num].Text, property.PropertyType));
num++;
}
其中 _textBoxes[num].text 是字符串
解决方案
我想在遍历 Student 类时跳过 ID 属性
可能会考虑创建一个 ViewModel,然后只使用所需的属性,例如
public class StudentViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string IDNumber { get; set; }
public string Mobile { get; set; }
public byte Grade { get; set; }
public int StatusID { get; set; }
}
推荐阅读
- javascript - 是否可以更改新遗物配置文件的根位置?
- javascript - 可排序的工作,但不可拖动
- javascript - DataTable 不适用于我的一张表
- javascript - 如何使用 gremlin javascript 发送日期
- sparql - 将数据从 S3 复制到 Neptune 实例 | 需要使用 SPARQL 查看我的数据
- jquery - 加载嵌入了 twitter 帖子的页面
- batch-file - 批处理:删除文件夹和子文件夹中所有文件的空行
- r - R:基于一列的两个条件子集数据帧
- c# - 调整组合框以大规模工作
- ajax - ajax回复工作但当它第二次加载时