public static bool IsValueEmptyCollection(this JsonProperty property, object target)
        {
            var value = property.ValueProvider.GetValue(target);

            if (value == null)
            {
                return(true);
            }

            var collection = value as ICollection;

            if (collection != null && collection.Count == 0)
            {
                return(true);
            }

            if (!_countAccessors.ContainsKey(property.PropertyType))
            {
                if (typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
                {
                    var countProperty = property.PropertyType.GetProperty("Count");
                    if (countProperty != null)
                    {
                        _countAccessors.AddOrUpdate(property.PropertyType, LateBinder.GetPropertyAccessor(countProperty));
                    }
                    else
                    {
                        _countAccessors.AddOrUpdate(property.PropertyType, null);
                    }
                }
                else
                {
                    _countAccessors.AddOrUpdate(property.PropertyType, null);
                }
            }

            var countAccessor = _countAccessors[property.PropertyType];

            if (countAccessor == null)
            {
                return(false);
            }

            var count = (int)countAccessor.GetValue(value);

            return(count == 0);
        }
示例#2
0
        public DataObjectConverter(IEnumerable <KeyValuePair <string, Type> > knownDataTypes = null)
        {
            if (knownDataTypes != null)
            {
                _dataTypeRegistry.AddRange(knownDataTypes);
            }

            if (_propertyAccessors.Count != 0)
            {
                return;
            }

            foreach (var prop in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.Public).Where(p => p.CanWrite))
            {
                _propertyAccessors.Add(prop.Name, LateBinder.GetPropertyAccessor(prop));
            }
        }