利用反射與 Lambda 表達式簡潔實作:
/// 獲得物件所有公開屬性名稱與值
public static Dictionary DynamicGetProperty(object obj)
{
Dictionary temp = new Dictionary();
Array.ForEach(obj.GetType().GetProperties(),
p => temp.Add(p.Name, p.GetValue(obj, null)));
return temp;
}
前文已完成動態類型建構、屬性值設定與取得的核心方法,奠定後續排序功能基礎。
為動態建立排序後的匿名類型做準備:
/// 取得型別所有公開屬性名稱與完整型別名稱
private static Dictionary TypeTransform(Type type)
{
Dictionary dictionary = new Dictionary();
Array.ForEach(type.GetProperties(),
p => dictionary.Add(p.Name, p.PropertyType.FullName));
return dictionary;
}
至此已完成四大核心能力:
下篇將運用這些基礎設施實作支援並列排名的 List 排序擴展方法。