【发布时间】:2011-02-08 11:40:50
【问题描述】:
我有一个带有 CustomerContacts 集合的 Customer 对象
IEnumerable<CustomerContact> Contacts { get; set; }
在其他一些代码中,我使用反射并拥有 Contacts 属性的 PropertyInfo
var contacts = propertyInfo.GetValue(customerObject, null);
我知道联系人中至少有一个对象,但我该如何取出呢?我不想将它转换为IEnumerable<CustomerContact>,因为我想让我的反射方法保持动态。我考虑过通过反射调用 FirstOrDefault() - 但不能轻易做到这一点,因为它是一种扩展方法。
有人有什么想法吗?
【问题讨论】:
-
正如我在问题中提到的,我需要保持反射方法动态。我已经减少了代码以仅显示问题,如果您在上下文中看到它,我怀疑您会建议这样做。
标签: c# reflection collections