public void InitPostData()
{
var options = GetOptions();
var type = this.GetType();
foreach (var option in options) {
var property = type.GetProperty(option);
if (property == null) {
throw new ArgumentException("Brew Error: Widget has option defined with no matching property.", option.Name);
}
var current = property.GetValue(this);
var postvalue = current;
var value = postvalue; // for use with the type converters, the end value.
var data = GetPostData();
if (data != null) {
// Changes were made on the client side for this widget, try to get the value for this option
data.TryGetValue(option.Name, out postvalue);
}
if (postvalue != null && postvalue is String) {
postvalue = System.Web.HttpUtility.HtmlDecode(postvalue as String);
}
if (postvalue == current || (postvalue != null && postvalue.Equals(current))) {
continue;
}
var attribute = property.GetCustomAttribute<TypeConverterAttribute>();
if (attribute != null) {
var converter = Activator.CreateInstance(Type.GetType(attribute.ConverterTypeName)) as TypeConverter;
if (converter != null) {
value = converter.CanConvertFrom(postvalue == null ? null : postvalue.GetType()) ? converter.ConvertFrom(postvalue) : postvalue;
}
else {
value = postvalue;
}
}
else {
value = postvalue;
}
try {
// jquery ui's defaults for string properties (and such) are often set to false.
if (value != null && value.Equals(false) && property.PropertyType == typeof(String)) {
value = option.DefaultValue;
}
property.SetValue(this, value);
}
catch (ArgumentException) {
// catches edge cases where there is no type converter defined. eg. false -> int[].
if (value.Equals(false) && option.DefaultValue == null) {
value = null;
}
property.SetValue(this, value);
}
}
}