private dynamic ConvertToModel(IDictionary <string, string> filters)
        {
            object model      = Activator.CreateInstance(FiltersModel);
            var    properties = FiltersModel.GetProperties();

            foreach (var property in properties.Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(PageFilter <>)))
            {
                var    targetType   = property.PropertyType.GetGenericArguments()[0];
                string propertyName = property.Name.ToLowerInvariant();

                var aliasAttribute = property.GetCustomAttributes(typeof(AliasAttribute), false).FirstOrDefault() as AliasAttribute;
                if (aliasAttribute != null)
                {
                    propertyName = aliasAttribute.Name;
                }

                var       pageFilterType   = typeof(PageFilter <>).MakeGenericType(targetType);
                object    targetValue      = UninitializePageFilterTypeActivator.CreateUnitializedObject(targetType);
                string    stringValue      = string.Empty;
                string    notSelectedValue = string.Empty;
                Exception exception        = null;

                if (!filters.ContainsKey(propertyName) || string.IsNullOrWhiteSpace(filters[propertyName]))
                {
                    object pageFilterNotSet = Activator.CreateInstance(pageFilterType, new[] { targetValue, false, null, string.Empty, string.Empty });
                    property.SetValue(model, pageFilterNotSet, null);
                    continue;
                }

                stringValue = filters[propertyName];

                try
                {
                    targetValue = PageFilterTypeConverter.Convert(stringValue, targetType);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }

                if (exception == null)
                {
                    var notSelectedValueAttribute =
                        property.GetCustomAttributes(typeof(NotSelectedValueAttribute), false).FirstOrDefault() as NotSelectedValueAttribute;
                    if (notSelectedValueAttribute != null)
                    {
                        notSelectedValue = notSelectedValueAttribute.NotSelectedValue;
                    }
                }

                object pageFilter = Activator.CreateInstance(pageFilterType, new[] { targetValue, exception == null, exception, stringValue, notSelectedValue });
                property.SetValue(model, pageFilter, null);
            }
            return(model);
        }
Exemple #2
0
        private static void ExtractRangeValues(Type propType, object value, out object lowerBound, out object upperBound)
        {
            if (value == null)
            {
                Type generic = propType.GetGenericArguments()[0];
                lowerBound = UninitializePageFilterTypeActivator.CreateUnitializedObject(generic);
                upperBound = lowerBound;
                return;
            }
            var lowerBoundProp = propType.GetProperty("LowerBound");
            var upperBoundProp = propType.GetProperty("UpperBound");

            lowerBound = lowerBoundProp.GetValue(value, null);
            upperBound = upperBoundProp.GetValue(value, null);
        }