Exemplo n.º 1
0
        private void BindParameter(ViewComponentParamAttribute paramAtt, PropertyInfo property, IConverter converter)
        {
            var compParamKey = string.IsNullOrEmpty(paramAtt.ParamName) ? property.Name : paramAtt.ParamName;

            var value = ComponentParams[compParamKey] ?? paramAtt.Default;

            if (value == null)
            {
                if (paramAtt.Required &&
                    (property.PropertyType.IsValueType || property.GetValue(this, null) == null))
                {
                    throw new ViewComponentException(string.Format("The parameter '{0}' is required by " +
                                                                   "the ViewComponent {1} but was not passed or had a null value",
                                                                   compParamKey, GetType().Name));
                }
            }
            else
            {
                try
                {
                    bool succeeded;

                    var converted = converter.Convert(property.PropertyType, value.GetType(), value, out succeeded);

                    if (succeeded)
                    {
                        property.SetValue(this, converted, null);
                    }
                    else
                    {
                        throw new Exception("Could not convert '" + value + "' to type " + property.PropertyType);
                    }
                }
                catch (Exception ex)
                {
                    throw new ViewComponentException(string.Format("Error trying to set value for parameter '{0}' " +
                                                                   "on ViewComponent {1}: {2}", compParamKey, GetType().Name,
                                                                   ex.Message), ex);
                }
            }
        }
Exemplo n.º 2
0
		private void BindParameter(ViewComponentParamAttribute paramAtt, PropertyInfo property, IConverter converter)
		{
			var compParamKey = string.IsNullOrEmpty(paramAtt.ParamName) ? property.Name : paramAtt.ParamName;

			var value = ComponentParams[compParamKey] ?? paramAtt.Default;

			if (value == null)
			{
				if (paramAtt.Required &&
				    (property.PropertyType.IsValueType || property.GetValue(this, null) == null))
				{
					throw new ViewComponentException(string.Format("The parameter '{0}' is required by " +
					                                               "the ViewComponent {1} but was not passed or had a null value",
					                                               compParamKey, GetType().Name));
				}
			}
			else
			{
				try
				{
					bool succeeded;

					var converted = converter.Convert(property.PropertyType, value.GetType(), value, out succeeded);

					if (succeeded)
					{
						property.SetValue(this, converted, null);
					}
					else
					{
						throw new Exception("Could not convert '" + value + "' to type " + property.PropertyType);
					}
				}
				catch (Exception ex)
				{
					throw new ViewComponentException(string.Format("Error trying to set value for parameter '{0}' " +
					                                               "on ViewComponent {1}: {2}", compParamKey, GetType().Name,
					                                               ex.Message), ex);
				}
			}
		}