Exemplo n.º 1
0
        /// <summary>
        /// Get dictionary of property values from an object
        /// </summary>
        /// <param name="annonymousObject">object to get properties from</param>
        /// <param name="values">collection to add to</param>
        /// <param name="casing">lowercase property names</param>
        /// <returns></returns>
        public static ImmutableHashTree <string, object> GetPropertiesFromObject(object annonymousObject,
                                                                                 ImmutableHashTree <string, object> values = null, PropertyCasing casing = PropertyCasing.Default)
        {
            values = values ?? ImmutableHashTree <string, object> .Empty;

            if (annonymousObject == null)
            {
                return(values);
            }

            if (annonymousObject.GetType() == typeof(IDictionary <string, object>))
            {
                return(((IDictionary <string, object>)annonymousObject).Aggregate(values,
                                                                                  (v, kvp) => v.Add(kvp.Key, kvp.Value)));
            }

            var objectType = annonymousObject.GetType();

            PropertyDictionaryDelegate propertyDelegate = null;

            switch (casing)
            {
            case PropertyCasing.Default:
                propertyDelegate = _propertyDelegates.GetValueOrDefault(objectType);
                break;

            case PropertyCasing.Lower:
                propertyDelegate = _lowerCasePropertyDelegates.GetValueOrDefault(objectType);
                break;

            case PropertyCasing.Upper:
                propertyDelegate = _upperCasePropertyDelegates.GetValueOrDefault(objectType);
                break;
            }

            if (propertyDelegate != null)
            {
                return(propertyDelegate(annonymousObject, values));
            }

            propertyDelegate = CreateDelegateForType(objectType, casing);

            switch (casing)
            {
            case PropertyCasing.Default:
                _propertyDelegates = _propertyDelegates.Add(objectType, propertyDelegate);
                break;

            case PropertyCasing.Lower:
                _lowerCasePropertyDelegates = _lowerCasePropertyDelegates.Add(objectType, propertyDelegate);
                break;

            case PropertyCasing.Upper:
                _upperCasePropertyDelegates = _upperCasePropertyDelegates.Add(objectType, propertyDelegate);
                break;
            }

            return(propertyDelegate(annonymousObject, values));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get dictionary of property values from an object
        /// </summary>
        /// <param name="annonymousObject">object to get properties from</param>
        /// <param name="values">collection to add to</param>
        /// <param name="casing">lowercase property names</param>
        /// <returns></returns>
        public static ImmutableHashTree <string, object> GetPropertiesFromObject(object annonymousObject,
                                                                                 ImmutableHashTree <string, object> values = null, PropertyCasing casing = PropertyCasing.Default)
        {
            values = values ?? ImmutableHashTree <string, object> .Empty;

            if (annonymousObject == null)
            {
                return(values);
            }

            if (annonymousObject is Array array)
            {
                var i = 0;

                foreach (var value in array)
                {
                    values = values.Add(i.ToString(), value);
                    i++;
                }

                return(values);
            }

            if (annonymousObject is IDictionary <string, object> dictionary)
            {
                return(dictionary.Aggregate(values,
                                            (v, kvp) => v.Add(kvp.Key, kvp.Value)));
            }

            var objectType = annonymousObject.GetType();

            PropertyDictionaryDelegate propertyDelegate = null;

            switch (casing)
            {
            case PropertyCasing.Default:
                propertyDelegate = _propertyDelegates.GetValueOrDefault(objectType);
                break;

            case PropertyCasing.Lower:
                propertyDelegate = _lowerCasePropertyDelegates.GetValueOrDefault(objectType);
                break;

            case PropertyCasing.Upper:
                propertyDelegate = _upperCasePropertyDelegates.GetValueOrDefault(objectType);
                break;
            }

            if (propertyDelegate != null)
            {
                return(propertyDelegate(annonymousObject, values));
            }

            propertyDelegate = CreateDelegateForType(objectType, casing);

            switch (casing)
            {
            case PropertyCasing.Default:
                ImmutableHashTree.ThreadSafeAdd(ref _propertyDelegates, objectType, propertyDelegate);
                break;

            case PropertyCasing.Lower:
                ImmutableHashTree.ThreadSafeAdd(ref _lowerCasePropertyDelegates, objectType, propertyDelegate);
                break;

            case PropertyCasing.Upper:
                ImmutableHashTree.ThreadSafeAdd(ref _upperCasePropertyDelegates, objectType, propertyDelegate);
                break;
            }

            return(propertyDelegate(annonymousObject, values));
        }