private static object GetFieldValue( KeyValuePair <YamlNode, YamlNode> fieldNodePair, FieldDeserializationInfo fieldDeserializationInfo, IErrorReporter errorReporter) { if (fieldDeserializationInfo.CustomMapperFunc != null) { return(fieldDeserializationInfo.CustomMapperFunc(fieldNodePair.Value.ToString(), fieldNodePair, errorReporter)); } if (fieldDeserializationInfo.Deserializer != null) { return(fieldDeserializationInfo.Deserializer.DeserializeObject((YamlMappingNode)fieldNodePair.Value, errorReporter)); } var propertyType = Nullable.GetUnderlyingType(fieldDeserializationInfo.PropertyInfo.PropertyType) ?? fieldDeserializationInfo.PropertyInfo.PropertyType; if (propertyType.IsEnum) { var parseSucceeded = System.Enum.TryParse( propertyType, fieldNodePair.Value.ToString(), out var enumValue); if (!parseSucceeded) { errorReporter.ReportError(fieldNodePair.Value, $"'{fieldNodePair.Value}' is not a valid value for '{fieldNodePair.Key}'."); } return(enumValue); } if (typeof(IDictionary <string, string>).IsAssignableFrom(propertyType)) { return(((YamlMappingNode)fieldNodePair.Value).GetDictionary()); } if (propertyType == typeof(TimeSpan)) { var parseSucceeded = TimeSpan.TryParse(fieldNodePair.Value.ToString(), out var timeSpanValue); if (!parseSucceeded) { errorReporter.ReportError(fieldNodePair.Value, $"'{fieldNodePair.Value}' is not a valid value for '{fieldNodePair.Key}'. The value must be in the format 'hh:mm:ss'."); } return(timeSpanValue); } try { return(Convert.ChangeType(fieldNodePair.Value.ToString(), fieldDeserializationInfo.PropertyInfo.PropertyType)); } catch (FormatException) { errorReporter.ReportError(fieldNodePair.Value, $"'{fieldNodePair.Value}' is not a valid value for '{fieldNodePair.Key}'. The value must be of type {fieldDeserializationInfo.PropertyInfo.PropertyType}."); } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="FieldDeserializationContext{TObject}"/> type. /// </summary> /// <param name="deserializationInfo">The information about the field to deserialize.</param> public FieldDeserializationContext(FieldDeserializationInfo deserializationInfo) { Guard.NotNull(deserializationInfo, nameof(deserializationInfo)); DeserializationInfo = deserializationInfo; }