首先,我知道流行的建议是避免完全返回空列表。但由于各种原因,现在我别无选择,只能这样做。
我的问题是如何通过对象的属性(可能是通过
我陷入了以下代码中,其中包括
我的问题是如何通过对象的属性(可能是通过
Reflection
)进行迭代,获取可能找到的任何列表并检查它是否为空。如果是,则将其转换为null
,否则保留不变。我陷入了以下代码中,其中包括
Reflection
的尝试:private static void IfEmptyListThenNull<T>(T myObject)
{
foreach (PropertyInfo propertyInfo in myObject.GetType().GetProperties())
{
if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
{
//How to know if the list i'm checking is empty, and set its value to null
}
}
}
dynamic
或反射调用.Count
属性不应该是一个问题... - Alexei Levenkov