简单点,就那个例子来说明。
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
//要排序的字段
var dynamicFieldName = "Last";
//获取字段的属性
var dynamicProp = typeof(Student).GetProperty(dynamicFieldName);
//获取排序后的结果
var sortedStudents = GetStudents().Select(u => u).OrderBy(u=>dynamicProp.GetValue(u, null));
//显示排序结果
foreach(s item in sortedStudents )
Console.WriteLine(s.First+" "+s.Last);
}
//获得所有学生的资料,放在一个List里
public static List<Student> GetStudents()
{
List<Student> students = new List<Student>
{
new Student {First="Donald ", Last="Trump", ID=1},
new Student {First="Barack", Last="Obama", ID=2},
new Student {First="Bill", Last="Clinton&#