コード例 #1
0
        internal static void InternalDeserialize <TParameters>(TParameters parameters, NameValueCollection collection, CultureInfo culture)
        {
            var serializableProperties =
                typeof(TParameters).GetProperties()
                .Where(p => IsSerializableProperty(p.PropertyType));

            foreach (var property in serializableProperties)
            {
                var name = GetPropertyName(property);

                if (collection.AllKeys.Select(e => e.ToUpper()).Contains(name.ToUpper()))
                {
                    string requestValue = collection[name];

                    try
                    {
                        object value = Conversion.AsValue(
                            property.PropertyType,
                            requestValue,
                            culture);

                        property.SetValue(parameters, value, null);
                    }
                    catch (Exception e)
                    {
                        throw new InvalidOperationException(
                                  "Error while converting parameter '" + property.Name + "' and value '" + requestValue + "'.", e);
                    }
                }
            }
        }
コード例 #2
0
        public void AsValue_MilitaryTime1800_ConvertsCorrectly()
        {
            var time     = (TimeSpan)Conversion.AsValue(typeof(TimeSpan), "18:00");
            var expected = new TimeSpan(18, 00, 00);

            Assert.AreEqual(expected, time);
        }
コード例 #3
0
        private static object GetCustomConvertedValue(ModelBindingContext bindingContext, Type propertyType)
        {
            var providerValue = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (providerValue != null && providerValue.AttemptedValue != null)
            {
                return(Conversion.AsValue(propertyType, providerValue.AttemptedValue));
            }

            return(null);
        }
コード例 #4
0
        private void SetPropertyValue(TRow row, ICsvColumn <TRow> column, CsvLine line, string element)
        {
            PropertyInfo prop = typeof(TRow).GetProperty(column.PropertyName);

            if (!this.Definition.IgnoreReadonlyProperties && !prop.CanWrite)
            {
                throw new CsvStreamMapperException("The field '" + column.PropertyName + "' is readonly (e.g. is not writable).", line);
            }
            if (prop.CanWrite)
            {
                prop.SetValue(row, Conversion.AsValue(prop.PropertyType, element, Definition.FormattingCulture), null);
            }
        }
コード例 #5
0
        public static T GetForm <T>(string name, CultureInfo culture)
        {
            try
            {
                HttpRequest request = GetCurrentRequest();

                string value = request.Form[name];

                return(Conversion.AsValue <T>(value, culture));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Could not retrieve form value with key '" + name + "'. See inner exception for details.", ex);
            }
        }
コード例 #6
0
        public static T QueryString <T>(string parameterName, CultureInfo culture)
        {
            HttpRequest request = GetCurrentRequest();

            string value = request.QueryString[parameterName];

            try
            {
                return(Conversion.AsValue <T>(value, culture));
            }
            catch (FormatException ex)
            {
                throw new FormatException("Cannot convert query parameter '" + parameterName + "' with value '" + value + "'.", ex);
            }
        }
コード例 #7
0
 public static TValue Value <TValue>(this TextBox control)
 {
     return(Conversion.AsValue <TValue>(control.Text));
 }
コード例 #8
0
 public static TValue SelectedValue <TValue>(this ListControl control)
 {
     return(Conversion.AsValue <TValue>(control.SelectedValue));
 }
コード例 #9
0
 public static IEnumerable <TValue> SelectedValues <TValue>(this ListControl control)
 {
     return(from item in control.Items.Cast <ListItem>()
            where item.Selected
            select Conversion.AsValue <TValue>(item.Value));
 }