private object GetPropertyValue(IModelProperty prop, IEnumerable values)
        {
            object result = null;

            if (prop.IsEnumerable)
            {
                result = values;
            }
            else if (prop.IsArray)
            {
                result = prop.ToArray(values);
            }
            else if (prop.IsCollection)
            {
                var tempValues = (IEnumerable)resolver.ResolveInstance(prop.PropertyType);
                foreach (var val in values)
                {
                    prop.AddToCollection(tempValues, val);
                }
                result = tempValues;
            }
            else //it's a single value, just return the first one (should really only be one thing)
            {
                result = values.Cast <object>().FirstOrDefault();
            }
            return(result);
        }
示例#2
0
        private object GetPropertyValue(IModelProperty prop, IEnumerable values)
        {
            object result = null;

            if (prop.IsEnumerable)
            {
                result = values;
            }
            else if (prop.IsArray)
            {
                result = prop.ToArray(values);
            }
            else if (prop.IsCollection)
            {
                var  tempValues = (IEnumerable)_resolver.ResolveInstance(prop.PropertyType);
                Type elementType;
                if (_resolver.ReflectionHelper.IsGenericCollection(prop.PropertyType, out elementType))
                {
                    foreach (var val in values)
                    {
                        if (elementType.IsAssignableFrom(val.GetType()))
                        {
                            prop.AddToCollection(tempValues, val);
                        }
                    }
                }

                result = tempValues;
            }
            else //it's a single value, just return the first one (should really only be one thing)
            {
                foreach (var val in values)
                {
                    if (val == null)
                    {
                        continue;
                    }

                    if (prop.PropertyType.IsAssignableFrom(val.GetType()))
                    {
                        result = val;
                        break;
                    }
                }
            }
            return(result);
        }