c# - 如何在 C# 中将属性作为参数传递?

标签 c# oop methods modular-design

我需要在 C# 中的同一对象类型中使用 4 个不同的属性来实现排序。

假设对象 Student 有姓名、ID、出生日期和年级。我如何重用代码来对它们进行排序。我已成功按名称排序,如何重用代码?

private void btnSortName_Click(object sender, EventArgs e)
    {
        Student obj = new Student();
        List<Student> listOfStudents = obj.List();
        int student_count = listOfStudents.Count();
        int first_index, last_index;

        for (first_index = 1; first_index < student_count; first_index++)
        {
            last_index = first_index - 1;
            Student first = listOfStudents[first_index];
            Student last = listOfStudents[last_index];

            while (last_index >= 0 && DateTime.Compare(last.RDate, first.RDate) > 0)
            {
                listOfStudents[last_index + 1] = listOfStudents[last_index];
                last_index = last_index - 1;
            }
            listOfStudents[last_index + 1] = first;
        }
        DataTable dt = Utility.ConvertToDataTable(listOfStudents);
        dataGridStudents.DataSource = dt;
        btnSortName.Visible = false;
        btnSortName.Enabled = false;
        btnSortNameD.Visible = true;
        btnSortNameD.Enabled = true;
    }

我尝试通过创建插入排序方法并将属性作为参数传递并返回该对象的列表来执行此操作,但这两个都显示错误:

public List<Student> insertion_Sort(ref String data, Boolean asc)
    {
        Student obj = new Student();
        List<Student> listOfStudents = obj.List();
        int student_count = listOfStudents.Count();
        int first_index, last_index;

        for (first_index = 1; first_index < student_count; first_index++)
        {
            last_index = first_index - 1;
            Student first = listOfStudents[first_index];
            Student last = listOfStudents[last_index];

            if (asc){
                while (last_index >= 0 && DateTime.Compare(last.data, first.data) > 0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
            else
            {
                while (last_index >= 0 && DateTime.Compare(last.data, first.data) < 0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
        }
        return listOfStudents;
    }

returntype

attribute as parameter

如何解决这些问题?

最佳答案

您的第二个错误是因为您无法执行 obj.attribute,而 attribute 是带有属性名称的字符串。使用诸如 obj.GetType().GetProperty(propertyName).GetValue(obj, null) 之类的内容来按名称获取属性..

也许这会起作用,尝试一下,因为我目前没有您的学生数据或 C# 编译器来检查。评论它输出的内容。

public List<Student> insertion_Sort(List<Student> listOfStudents,ref String propertyName, Boolean asc)
    {
        Student obj = listOfStudents [0];
        int student_count = listOfStudents.Count();
        int first_index, last_index;
        dynamic prop= obj.GetType().GetProperty(propertyName);
        for (first_index = 1; first_index < student_count; first_index++)
        {
            last_index = first_index - 1;
            Student first = listOfStudents[first_index];
            Student last = listOfStudents[last_index];

            if (asc){
                while (last_index >= 0 && prop.GetValue(first, null)-prop.GetValue(last, null) > 0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
            else
            {
                while (last_index >= 0 && prop.GetValue(first, null)-prop.GetValue(last, null) <0)
                {
                    listOfStudents[last_index + 1] = listOfStudents[last_index];
                    last_index = last_index - 1;
                }
                listOfStudents[last_index + 1] = first;
            }
        }
        return listOfStudents;
    }

关于c# - 如何在 C# 中将属性作为参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59573155/

相关文章:

c# - 我应该更喜欢封闭式还是开放式列表<>系统?

c# - 当父控件包含嵌套按钮时如何阻止父控件接收触摸事件

javascript - 有人可以向我解释为什么 javascript 中的这种继承不起作用吗?

java - 你会如何设计协作/可共享的文本编辑器 : Key points are given below

java - 从列表中读取文件

java - 执行 java 方法所花费的时间为零?

Ruby Test::Unit 当前的测试方法名

c# - Web API 系统内存不足异常

c# - 使用 System.Threading 使用 50% CPU 的简单 .exe

java - 如何实现可配置的单例?