public void CIFRecordFieldConvertToDateTimeAttributesValid() { Assembly cifRecordAssembly = typeof(CIFRecord).Assembly; Type[] cifRecordClasses = cifRecordAssembly.GetTypes() .Where(type => type.IsSubclassOf(typeof(CIFRecord))) .Where(type => type.IsClass && !type.IsAbstract) .ToArray(); foreach (Type cifRecordClass in cifRecordClasses) { PropertyInfo[] cifRecordFieldProperties = cifRecordClass.GetProperties() .Where(property => property.IsDefined(typeof(CIFRecordFieldLocationAttribute), false)) .Where(property => property.IsDefined(typeof(CIFRecordFieldConvertToDateTimeAttribute), false)) .ToArray(); for (int fieldIndex = 0; fieldIndex < cifRecordFieldProperties.Length; fieldIndex++) { CIFRecordFieldLocationAttribute cifRecordFieldLocationAttribute = (CIFRecordFieldLocationAttribute)cifRecordFieldProperties[fieldIndex].GetCustomAttribute(typeof(CIFRecordFieldLocationAttribute)); CIFRecordFieldConvertToDateTimeAttribute cifRecordFieldConvertToDateTimeAttribute = (CIFRecordFieldConvertToDateTimeAttribute)cifRecordFieldProperties[fieldIndex].GetCustomAttribute(typeof(CIFRecordFieldConvertToDateTimeAttribute)); Assert.AreEqual(cifRecordFieldLocationAttribute.length, cifRecordFieldConvertToDateTimeAttribute.format.Length, $"Property: {cifRecordClass.FullName}.{cifRecordFieldProperties[fieldIndex].Name}"); } } }
public static CIFRecord ParseRecord(string recordString, CIFRecordIdentity cifRecordIdentity) { Type cifRecordClass = CIFRecordIdentityToRecordClass(cifRecordIdentity); dynamic recordToReturn = Activator.CreateInstance(cifRecordClass); foreach (PropertyInfo property in cifRecordClass.GetProperties()) { CIFRecordFieldLocationAttribute cifRecordFieldLocationAttribute = (CIFRecordFieldLocationAttribute)Attribute.GetCustomAttribute(property, typeof(CIFRecordFieldLocationAttribute)); if (cifRecordFieldLocationAttribute != null) { CIFRecordFieldConvertToBoolAttribute cifRecordFieldConvertToBoolAttribute = (CIFRecordFieldConvertToBoolAttribute)Attribute.GetCustomAttribute(property, typeof(CIFRecordFieldConvertToBoolAttribute)); CIFRecordFieldConvertToDateTimeAttribute cifRecordFieldConvertToDateTimeAttribute = (CIFRecordFieldConvertToDateTimeAttribute)Attribute.GetCustomAttribute(property, typeof(CIFRecordFieldConvertToDateTimeAttribute)); string fieldStringValue = recordString.Substring(cifRecordFieldLocationAttribute.startPosition - 1, cifRecordFieldLocationAttribute.length); object propertyValue; if (cifRecordFieldConvertToBoolAttribute != null) { if (fieldStringValue == cifRecordFieldConvertToBoolAttribute.trueString) { propertyValue = true; } else if (fieldStringValue == cifRecordFieldConvertToBoolAttribute.falseString) { propertyValue = false; } else if (string.IsNullOrWhiteSpace(fieldStringValue)) { propertyValue = false; } else { throw new Exception("Malformed record."); } } else if (cifRecordFieldConvertToDateTimeAttribute != null) { try { propertyValue = DateTime.ParseExact(fieldStringValue, cifRecordFieldConvertToDateTimeAttribute.format, CultureInfo.InvariantCulture); } catch (FormatException) { if (fieldStringValue == "99999999") { propertyValue = null; } else { throw new Exception("Invalid DateTime value."); } } } else { propertyValue = fieldStringValue; } property.SetValue(recordToReturn, propertyValue); } } return(recordToReturn); }