首页 > 解决方案 > 如何获取子属性的 PropertyDescriptor

问题描述

我需要从字符串中获取对象的子属性的 PropertyDescriptor。为了之后根据该属性对集合进行排序。

我为该方法提供了一个类似“Subobject.Subproperty”的字符串,但 PropertyDescriptor 为空

PropertyDescriptor prop = TypeDescriptor.GetProperties(typeof(MyParentObject)).Find("MyChildObject.MySubProperty", true);
//....
items = items.OrderBy(y => prop.GetValue(y)).ToList();

最后我得到一个由 prop 为空引起的异常。

标签: c#.net

解决方案


推荐阅读