Esempio n. 1
0
        public static IDictionary<string, object> ObjectToDictionary(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException(nameof(obj));

            return FastProperty.ObjectToDictionary(
                obj,
                key => key.Replace("_", "-").Replace("@", ""));
        }
        /// <summary>
        /// </summary>
        public IEnumerable <FastProperty> GetSignatureProperties()
        {
            var type          = GetType();
            var propertyNames = GetSignaturePropertyNamesCore();

            foreach (var name in propertyNames)
            {
                var fastProperty = FastProperty.GetProperty(type, name);
                if (fastProperty != null)
                {
                    yield return(fastProperty);
                }
            }
        }
        public static void Populate(IDictionary <string, object> source, object target, out ICollection <ConvertProblem> problems, params object[] populated)
        {
            Guard.NotNull(source, nameof(source));
            Guard.NotNull(target, nameof(target));

            problems = new List <ConvertProblem>();

            if (populated.Any(x => x == target))
            {
                return;
            }

            Type t = target.GetType();

            if (source != null)
            {
                foreach (var fastProp in FastProperty.GetProperties(t).Values)
                {
                    object value;
                    var    pi = fastProp.Property;

                    if (!pi.PropertyType.IsPredefinedSimpleType() && source.TryGetValue(pi.Name, out value) && value is IDictionary <string, object> )
                    {
                        var nestedValue = fastProp.GetValue(target);
                        ICollection <ConvertProblem> nestedProblems;

                        populated = populated.Concat(new object[] { target }).ToArray();
                        Populate((IDictionary <string, object>)value, nestedValue, out nestedProblems, populated);

                        if (nestedProblems != null && nestedProblems.Any())
                        {
                            problems.AddRange(nestedProblems);
                        }
                        WriteToProperty(target, fastProp, nestedValue, problems);
                    }
                    else if (pi.PropertyType.IsArray && !source.ContainsKey(pi.Name))
                    {
                        WriteToProperty(target, fastProp, RetrieveArrayValues(pi, source, problems), problems);
                    }
                    else
                    {
                        if (source.TryGetValue(pi.Name, out value))
                        {
                            WriteToProperty(target, fastProp, value, problems);
                        }
                    }
                }
            }
        }
        // REVIEW: Dieser Code ist redundant mit DefaultModelBinder u.Ä. Entweder ablösen oder eliminieren (vielleicht ist es ja in diesem Kontext notwendig!??!)
        private static object RetrieveArrayValues(PropertyInfo arrayProp, IDictionary <string, object> source, ICollection <ConvertProblem> problems)
        {
            Type elemType       = arrayProp.PropertyType.GetElementType();
            bool anyValuesFound = true;
            int  idx            = 0;
            var  elements       = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(elemType));

            var properties = FastProperty.GetProperties(elemType);

            while (anyValuesFound)
            {
                object curElement = null;
                anyValuesFound = false; // false until proven otherwise

                foreach (var prop in properties.Values)
                {
                    //var key = string.Format("_{0}{1}_{2}", idx, arrayProp.Name, pd.Name);
                    var    key = string.Format("{0}[{1}].{2}", arrayProp.Name, idx, prop.Name);
                    object value;

                    if (source.TryGetValue(key, out value))
                    {
                        anyValuesFound = true;

                        if (curElement == null)
                        {
                            curElement = Activator.CreateInstance(elemType);
                            elements.Add(curElement);
                        }

                        SetPropFromValue(value, curElement, prop, problems);
                    }
                }

                idx++;
            }

            var elementArray = Array.CreateInstance(elemType, elements.Count);

            elements.CopyTo(elementArray, 0);

            return(elementArray);
        }
        private static void WriteToProperty(object item, FastProperty prop, object value, ICollection <ConvertProblem> problems)
        {
            var pi = prop.Property;

            if (!pi.CanWrite)
            {
                return;
            }

            try
            {
                if (value != null && !Equals(value, ""))
                {
                    Type destType = pi.PropertyType;

                    if (destType == typeof(bool) && Equals(value, pi.Name))
                    {
                        value = true;
                    }

                    if (pi.PropertyType.IsAssignableFrom(value.GetType()))
                    {
                        prop.SetValue(item, value);
                        return;
                    }

                    if (pi.PropertyType.IsNullable(out var wrappedType))
                    {
                        destType = wrappedType;
                    }

                    prop.SetValue(item, value.Convert(destType));
                }
            }
            catch (Exception ex)
            {
                problems.Add(new ConvertProblem {
                    Item = item, Property = pi, AttemptedValue = value, Exception = ex
                });
            }
        }
Esempio n. 6
0
        private static bool TryGetCachedProperty(
            Type type,
            string propertyName,
            bool eagerCached,
            out FastProperty fastProperty)
        {
            fastProperty = null;
            IDictionary <string, FastProperty> allProperties;

            if (eagerCached)
            {
                allProperties = (IDictionary <string, FastProperty>)GetProperties(type);
                allProperties.TryGetValue(propertyName, out fastProperty);
            }

            if (fastProperty == null && _propertiesCache.TryGetValue(type, out allProperties))
            {
                allProperties.TryGetValue(propertyName, out fastProperty);
            }

            return(fastProperty != null);
        }
 private static void SetPropFromValue(object value, object item, FastProperty prop, ICollection <ConvertProblem> problems)
 {
     WriteToProperty(item, prop, value, problems);
 }