我很想知道,是否有更简单的方式来做这样的事情?
public int NonNullPropertiesCount(object entity)
{
if (entity == null) throw new ArgumentNullException("A null object was passed in");
int nonNullPropertiesCount = 0;
Type entityType = entity.GetType();
foreach (var property in entityType.GetProperties())
{
if (property.GetValue(entity, null) != null)
nonNullPropertiesCount = nonNullPropertiesCount+ 1;
}
return nonNullPropertiesCount;
}