public static object StringToField(this IFieldInfoDescriptor fieldDescriptor, string value) { if (value == null) { return(null); } value = fieldDescriptor.StringTrim(value); if (string.Empty.Equals(value) && fieldDescriptor.Converter == null) { return(value); } if (fieldDescriptor.Converter == null && fieldDescriptor.Type == null) { return(value); } ConverterBase converterInstance = fieldDescriptor.Converter == null ? ConverterFactory.GetDefaultConverter(fieldDescriptor.Type, fieldDescriptor.ConverterFormat) : ConverterFactory.GetConverter(fieldDescriptor.Converter, fieldDescriptor.ConverterFormat); return(converterInstance == null ? value : converterInstance.StringToField(value)); }
internal static object StringToRecord(this IFieldInfoDescriptor fieldInfoDescriptor, string fieldString, char nullChar) { if (fieldString == null) { return(fieldInfoDescriptor.NullValue ?? null); } string stringNullRepresentation = new string(nullChar, fieldString.Length); if (fieldString == stringNullRepresentation) { return(fieldInfoDescriptor.NullValue ?? null); } fieldString = fieldInfoDescriptor.StringTrim(fieldString); ConverterBase converterInstance; if (string.Empty.Equals(fieldString) && fieldInfoDescriptor.Converter == null) { if (fieldInfoDescriptor.NullValue != null) { fieldString = fieldInfoDescriptor.NullValue.ToString(); } if (string.Empty.Equals(fieldString) && fieldInfoDescriptor.Converter == null) { if (fieldInfoDescriptor.Type != null) { converterInstance = ConverterFactory.GetDefaultConverter(fieldInfoDescriptor.Type, fieldInfoDescriptor.ConverterFormat); return(converterInstance == null ? fieldString : converterInstance.StringToField(fieldString)); } return(fieldString); } } if (fieldInfoDescriptor.Converter == null && fieldInfoDescriptor.Type == null) { return(fieldString); } if (string.IsNullOrWhiteSpace(fieldString) && fieldInfoDescriptor.NullValue != null) { fieldString = fieldInfoDescriptor.NullValue.ToString(); } converterInstance = fieldInfoDescriptor.Converter == null ? ConverterFactory.GetDefaultConverter(fieldInfoDescriptor.Type, fieldInfoDescriptor.ConverterFormat) : ConverterFactory.GetConverter(fieldInfoDescriptor.Converter, fieldInfoDescriptor.ConverterFormat); return(converterInstance == null ? fieldString : converterInstance.StringToField(fieldString)); }
public static string ResolveType(this IFieldInfoDescriptor fieldDescriptor) { if (fieldDescriptor.Type == null && fieldDescriptor.Converter == null) { return("string"); } var converterInstance = fieldDescriptor.Converter == null ? ConverterFactory.GetDefaultConverter(fieldDescriptor.Type, fieldDescriptor.ConverterFormat) : ConverterFactory.GetConverter(fieldDescriptor.Converter, fieldDescriptor.ConverterFormat); if (converterInstance == null) { return("string"); } return(converterInstance.FieldType); }
public static string CreateFieldString(this IFieldInfoDescriptor fieldBuilder, object fieldValue) { ConverterBase converterInstance = null; if (fieldBuilder.Type != null || fieldBuilder.Converter != null) { converterInstance = fieldBuilder.Converter == null ? ConverterFactory.GetDefaultConverter(fieldBuilder.Type, fieldBuilder.ConverterFormat) : ConverterFactory.GetConverter(fieldBuilder.Converter, fieldBuilder.ConverterFormat); } if (converterInstance == null) { if (fieldValue == null) { return(string.Empty); } return(fieldValue.ToString()); } return(converterInstance.FieldToString(fieldValue) ?? string.Empty); }
public static object StringToRecord(this IFixedFieldInfoDescriptor recordInfo, string line, ref int offset) { if (offset >= line.Length) { return(null); } int length = recordInfo.Length; if (line.Length < recordInfo.Length + offset) { length = line.Length - offset; } var stringValue = line.Substring(offset, length); offset += length; string stringNullRepresentation = new string('\u0000', length); if ((stringValue == null || stringValue == stringNullRepresentation) && recordInfo.NullValue == null) { return(null); } else if ((stringValue == null || stringValue == stringNullRepresentation) && recordInfo.NullValue != null) { stringValue = recordInfo.NullValue.ToString(); } stringValue = recordInfo.StringTrim(stringValue); ConverterBase converterInstance; if (string.Empty.Equals(stringValue) && recordInfo.Converter == null) { if (recordInfo.NullValue != null) { stringValue = recordInfo.NullValue.ToString(); } if (string.Empty.Equals(stringValue) && recordInfo.Converter == null) { if (recordInfo.Type != null) { converterInstance = ConverterFactory.GetDefaultConverter(recordInfo.Type, recordInfo.ConverterFormat); return(converterInstance == null ? stringValue : converterInstance.StringToField(stringValue)); } return(stringValue); } } if (recordInfo.Converter == null && recordInfo.Type == null) { return(stringValue); } if (string.IsNullOrWhiteSpace(stringValue) && recordInfo.NullValue != null) { stringValue = recordInfo.NullValue.ToString(); } converterInstance = recordInfo.Converter == null ? ConverterFactory.GetDefaultConverter(recordInfo.Type, recordInfo.ConverterFormat) : ConverterFactory.GetConverter(recordInfo.Converter, recordInfo.ConverterFormat); return(converterInstance == null ? stringValue : converterInstance.StringToField(stringValue)); }
/// <summary> /// Create a field base from a fieldinfo object /// Verify the settings against the actual field to ensure it will work. /// </summary> /// <param name="fi">Field Info Object</param> protected FieldBase(FieldInfo fi, string delimiter) : this() { FieldInfo = fi; FieldType = FieldInfo.FieldType; MemberInfo attibuteTarget = fi; FieldFriendlyName = AutoPropertyName(fi); if (string.IsNullOrEmpty(FieldFriendlyName) == false) { var prop = fi.DeclaringType.GetProperty(FieldFriendlyName); if (prop == null) { FieldFriendlyName = null; } else { IsAutoProperty = true; attibuteTarget = prop; } } FieldTypeInternal = FieldType.IsArray ? FieldType.GetElementType() : FieldType; IsStringField = FieldTypeInternal == typeof(string); var attributes = attibuteTarget.GetCustomAttributes(typeof(FieldConverterAttribute), true); foreach (var attribute in attributes) { var fc = attribute as FieldConverterAttribute; if (fc != null) { fc.Delimiter = delimiter; } #if DEBUG var del = attribute as DelimitedRecordAttribute; if (del != null) { Debug.Assert(del.Delimiter == delimiter); } #endif } if (attributes.Length > 0) { var conv = (FieldConverterAttribute)attributes[0]; Converter = conv.Converter; conv.ValidateTypes(FieldInfo); } else { Converter = ConverterFactory.GetDefaultConverter(FieldFriendlyName ?? fi.Name, FieldType); } if (Converter != null) { if (Converter.Type == null) { throw new ArgumentNullException(nameof(IFieldConverter.Type), $"Converter type [{Converter.GetType().Name}] can not be null!"); } if (Converter.Type != FieldTypeInternal) { throw new TypeMismatchException(Converter.Type, $"{Converter.GetType().Name} destination type {Converter.Type} != expected type {FieldTypeInternal}"); } //Converter.mDestinationType = FieldTypeInternal; } attributes = attibuteTarget.GetCustomAttributes(typeof(FieldNullValueAttribute), true); if (attributes.Length > 0) { NullValue = ((FieldNullValueAttribute)attributes[0]).NullValue; // mNullValueOnWrite = ((FieldNullValueAttribute) attribs[0]).NullValueOnWrite; if (NullValue != null) { if (!FieldTypeInternal.IsAssignableFrom(NullValue.GetType())) { throw new BadUsageException( $"The NullValue is of type: {NullValue.GetType().Name} which is not asignable to the field {FieldInfo.Name} Type: {FieldTypeInternal.Name}"); } } } IsNullableType = FieldTypeInternal.IsValueType && FieldTypeInternal.IsGenericType && FieldTypeInternal.GetGenericTypeDefinition() == typeof(Nullable <>); }