c# – 使用反射生成迭代器
发布时间:2020-12-15 20:49:58 所属栏目:百科 来源:网络整理
导读:我有一个数据类学生,我有一个聚合类学生. Student有两个类型为string的属性:Name和City. 我想要做的是选择使用foreach机制选择要迭代的属性. 我写的代码有效,它也可读,外观漂亮. 主要问题是性能:我使用yield关键字的行可能不是很有效,但问题是多少?这是戏
我有一个数据类学生,我有一个聚合类学生.
Student有两个类型为string的属性:Name和City. 我想要做的是选择使用foreach机制选择要迭代的属性. 我写的代码有效,它也可读,外观漂亮. 有没有更好的方法来实现此功能? static void Main(string[] args) { Students students = new Students(); students.AddStudent(new Student { Age = 20,Name = "Stud1",City="City1" }); students.AddStudent(new Student { Age = 46,Name = "Stud2",City="City2"}); students.AddStudent(new Student { Age = 32,Name = "Stud3",City="City3" }); students.AddStudent(new Student { Age = 34,Name = "Stud4",City="city4" }); students.PropertyToIterate = eStudentProperty.City; foreach (string studentCity in students) { Console.WriteLine(studentcity); } students.PropertyToIterate = eStudentProperty.Name; foreach (string studentName in students) { Console.WriteLine(studentName); } } public class Students :IEnumerable<object> { private List<Student> m_Students = new List<Student>(); private eStudentProperty m_PropertyToIterate = eStudentProperty.Name; public eStudentProperty PropertyToIterate { get { return m_PropertyToIterate; } set { m_PropertyToIterate = value; } } public void AddStudent(Student i_Student) { m_Students.Add(i_Student); } public IEnumerator<object> GetEnumerator() { for (int i = 0; i < m_Students.Count; ++i) { yield return (object)m_Students[i].GetType().GetProperty(PropertyToIterate.ToString()).GetValue(m_Students[i],null); } } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } } public enum eStudentProperty { Name,Age,City } public class Student { public string Name { get; set; } public string City { get; set; } public int Age { get; set; } } 解决方法
为了回应你的编辑,这样的事情怎么样……
Students students = new Students(); students.AddStudent(new Student { Age = 20,City = "City1" }); students.AddStudent(new Student { Age = 46,City = "City2" }); students.AddStudent(new Student { Age = 32,City = "City3" }); students.AddStudent(new Student { Age = 34,City = "city4" }); foreach (int studentAge in students.EnumerateBy(StudentProperty.Age)) { Console.WriteLine(studentAge); } foreach (string studentName in students.EnumerateBy(StudentProperty.Name)) { Console.WriteLine(studentName); } foreach (string studentCity in students.EnumerateBy(StudentProperty.City)) { Console.WriteLine(studentCity); } // ... public class Students { private List<Student> _students = new List<Student>(); public void AddStudent(Student student) { _students.Add(student); } public IEnumerable<T> EnumerateBy<T>(StudentProperty<T> property) { return _students.Select(property.Selector); } } public static class StudentProperty { public static readonly StudentProperty<int> Age = new StudentProperty<int>(s => s.Age); public static readonly StudentProperty<string> Name = new StudentProperty<string>(s => s.Name); public static readonly StudentProperty<string> City = new StudentProperty<string>(s => s.City); } public sealed class StudentProperty<T> { internal Func<Student,T> Selector { get; private set; } internal StudentProperty(Func<Student,T> selector) { Selector = selector; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |