private void BindParameter(ViewComponentParamAttribute paramAtt, PropertyInfo property, IConverter converter)
{
string compParamKey = string.IsNullOrEmpty(paramAtt.ParamName) ? property.Name : paramAtt.ParamName;
object value = ComponentParams[compParamKey];
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;
object 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);
}
}
}