【问题标题】:How to pass a attribute as parameter in C#?如何在 C# 中将属性作为参数传递?
【发布时间】:2020-04-21 16:44:58
【问题描述】:

我需要在 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;
    }

我该如何解决这些问题?

【问题讨论】:

  • 也许你可以使用一个联合数据结构,它可以获取类的所有属性。然后在 insert_sort 方法开始时,创建两个变量,它们将采用相应对象的属性并将它们与 &lt; 和 &gt; 进行比较,而不是仅适用于日期的日期时间比较(如果有,则将日期时间转换为刻度数其他问题)。但我觉得最好做个表格,学生属性作为行数据,然后按列排序。
  • 您必须将class Student 和public List&lt;Student&gt; insertion_Sort 方法访问修饰符调整为相同。 class Student 和 insertion_Sort 都应该具有相同的访问修饰符(如 public 或 private 或 protected)

标签: c# oop methods modular-design


【解决方案1】:

自定义排序的标准方法是向排序算法提供IComparer&lt;T&gt;。

  • 修改您的排序以接受IComparer&lt;Student&gt;。通过调用comparer.Compare() 替换排序中的比较。作为奖励,您可以使您的排序通用。

  • 为您希望对 Student 对象进行排序的每种方式创建 IComparer&lt;Student&gt; 的实现。最简单的方法是使用Comparer.Create():

    var nameComparer = Comparer<Student>.Create((studentA, studentB) => string.Compare(studentA.Name, studentB.Name));
    var gradeComparer = Comparer<Student>.Create((studentA, studentB) => studentA.grade.CompareTo(studentB.grade));
    

这与List&lt;T&gt;.Sort() 和Array.Sort() 允许自定义排序的方式相同。

【讨论】:

    【解决方案2】:

    您的第二个错误是因为您不能执行 obj.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;
        }
    

    【讨论】:

      猜你喜欢
      • 2010-11-25
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多