public void CIFRecordFieldConvertToBoolAttributesValid()
        {
            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(CIFRecordFieldConvertToBoolAttribute), false))
                                                          .ToArray();

                for (int fieldIndex = 0; fieldIndex < cifRecordFieldProperties.Length; fieldIndex++)
                {
                    CIFRecordFieldLocationAttribute      cifRecordFieldLocationAttribute      = (CIFRecordFieldLocationAttribute)cifRecordFieldProperties[fieldIndex].GetCustomAttribute(typeof(CIFRecordFieldLocationAttribute));
                    CIFRecordFieldConvertToBoolAttribute cifRecordFieldConvertToBoolAttribute = (CIFRecordFieldConvertToBoolAttribute)cifRecordFieldProperties[fieldIndex].GetCustomAttribute(typeof(CIFRecordFieldConvertToBoolAttribute));

                    Assert.AreEqual(cifRecordFieldLocationAttribute.length, cifRecordFieldConvertToBoolAttribute.trueString.Length, $"Property: {cifRecordClass.FullName}.{cifRecordFieldProperties[fieldIndex].Name}");
                    Assert.AreEqual(cifRecordFieldLocationAttribute.length, cifRecordFieldConvertToBoolAttribute.falseString.Length, $"Property: {cifRecordClass.FullName}.{cifRecordFieldProperties[fieldIndex].Name}");
                }
            }
        }
Пример #2
0
        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);
        }