public static void ConvertNSetValue(this object target, PropertyDescriptor pd, object fv, CultureInfo culture, long index = 0) { PropertyInfo pi = pd.ComponentType.GetProperty(pd.Name); IChoNotifyRecordFieldRead callbackRecord = target as IChoNotifyRecordFieldRead; if (callbackRecord != null) { object state = fv; bool retValue = ChoFuncEx.RunWithIgnoreError(() => callbackRecord.BeforeRecordFieldLoad(target, index, pd.Name, ref state), true); if (retValue) { fv = state; } } try { var tpi = GetTypePropertyInfo(pd.ComponentType, pi); object[] propConverters = tpi.PropConverters; object[] propConverterParams = tpi.PropConverterParams; if (propConverters.IsNullOrEmpty()) { fv = ChoConvert.ConvertFrom(fv, pi.PropertyType, null, propConverters, propConverterParams, culture); } else { fv = ChoConvert.ConvertFrom(fv, pi.PropertyType, null, propConverters, propConverterParams, culture); } pd.SetValue(target, fv); if (callbackRecord != null) { ChoFuncEx.RunWithIgnoreError(() => callbackRecord.AfterRecordFieldLoad(target, index, pd.Name, fv), true); } } catch (Exception ex) { if (callbackRecord != null) { bool ret = ChoFuncEx.RunWithIgnoreError(() => callbackRecord.RecordFieldLoadError(target, index, pd.Name, ref fv, ex), false); if (!ret) { if (ex is ValidationException) { throw; } throw new ChoReaderException($"Failed to parse '{fv}' value for '{pd.Name}' field in '{target.GetType().Name}' object.", ex); } else { pd.SetValue(target, fv); } } } }
private bool RaiseBeforeRecordFieldLoad(object target, long index, string propName, ref object value) { if (_callbackFieldRecord != null) { object state = value; bool retValue = ChoFuncEx.RunWithIgnoreError(() => _callbackFieldRecord.BeforeRecordFieldLoad(target, index, propName, ref state), true); if (retValue) { value = state; } return(retValue); } else if (target is IChoNotifyRecordFieldRead) { object state = value; bool retValue = ChoFuncEx.RunWithIgnoreError(() => ((IChoNotifyRecordFieldRead)target).BeforeRecordFieldLoad(target, index, propName, ref state), true); if (retValue) { value = state; } return(retValue); } else if (Reader != null) { object state = value; bool retValue = ChoFuncEx.RunWithIgnoreError(() => Reader.RaiseBeforeRecordFieldLoad(target, index, propName, ref state), true); if (retValue) { value = state; } return(retValue); } return(true); }