/// <summary> /// Set the value of the property for this FieldMap on the given instance. /// </summary> /// <param name="instance">The object whose property value will be set.</param> /// <param name="value">The value to assign to the property.</param> public void SetValue(object instance, object value) { if (IsNull(value)) { value = NullValue; Check.Verify(IsNullAssignable || NullValue != null, Error.NullWithNoNullValue, columnName, map is ObjectMap ? (map as ObjectMap).Type.Name : map.TableName); } // perform a few automatic type conversions else if (memberType.IsEnum) { value = handleEnumAsString ? Enum.Parse(memberType, Convert.ToString(value), true) : Enum.ToObject(memberType, Convert.ToInt32(value)); } else if (memberType == typeof(Guid) && value != null && value.GetType() == typeof(string)) { if (Size == 16) // binary compressed version { value = TypeConverter.ToGuid((string)value); } else { value = new Guid((string)value); } } else if (memberType == typeof(decimal) && value != null && value.GetType() == typeof(string)) { value = Convert.ToDecimal(value, NumberFormatInfo.InvariantInfo); } else { System.ComponentModel.TypeConverter typeConv = TypeDescriptor.GetConverter(memberType); if (typeConv != null && typeConv.CanConvertFrom(value.GetType())) { value = typeConv.ConvertFrom(value); } else { // check for the existence of a TypeConverterAttribute for the field/property object[] attrs = MemberInfo.GetCustomAttributes(typeof(TypeConverterAttribute), false); if (attrs.Length == 1) { TypeConverterAttribute tca = (TypeConverterAttribute)attrs[0]; System.ComponentModel.TypeConverter typeConverter = (System.ComponentModel.TypeConverter) Activator.CreateInstance(Type.GetType(tca.ConverterTypeName)); if (value != null && typeConverter != null && typeConverter.CanConvertFrom(value.GetType())) { value = typeConverter.ConvertFrom(value); } else { value = Convert.ChangeType(value, memberType); } } else { value = Convert.ChangeType(value, memberType); } } } // update member if (memberInfo is PropertyInfo) { (memberInfo as PropertyInfo).SetValue(instance, value, Reflector.InstanceCriteria, null, null, null); } else { (memberInfo as FieldInfo).SetValue(instance, value, Reflector.InstanceCriteria, null, null); } }