示例#1
0
        public AbstractLisRecord(string aLisString)
        {
            bool isSubRecord = this is AbstractLisSubRecord;
            char sepChar     = ((!isSubRecord) ? LISDelimiters.FieldDelimiter : LISDelimiters.ComponentDelimiter);
            Type selfType    = this?.GetType();

            PropertyInfo[] props = selfType.GetProperties();
            int            limit = int.MaxValue;

            if (isSubRecord && (int)props.LongLength > 0 && !props[(int)props.LongLength - 1].PropertyType.IsArray)
            {
                limit = props.Length;
            }
            RecordFields rf = new RecordFields(aLisString, sepChar, limit);
            int          i  = 0;

            PropertyInfo[] array = props;
            if (array == null)
            {
                return;
            }
            for (; i < (int)array.LongLength; i++)
            {
                PropertyInfo prop    = array[i];
                object[]     attribs = prop.GetCustomAttributes(typeof(LisRecordFieldAttribute), inherit: false);
                if ((int)attribs.LongLength <= 0)
                {
                    continue;
                }
                LisRecordFieldAttribute attrib = (LisRecordFieldAttribute)attribs[0];
                string field = rf.GetField(attrib.FieldIndex);
                if (string.IsNullOrEmpty(field))
                {
                    continue;
                }
                Type propType         = prop.PropertyType;
                Type nullablePropType = Nullable.GetUnderlyingType(propType);
                if ((object)nullablePropType != null)
                {
                    propType = nullablePropType;
                }
                if (!(propType == typeof(int)))
                {
                    if (!(propType == typeof(string)))
                    {
                        if (propType == typeof(DateTime))
                        {
                            LisDateTimeUsage dateTimeUsage = LisDateTimeUsage.DateTime;
                            attribs = prop.GetCustomAttributes(typeof(LisDateTimeUsageAttribute), inherit: false);
                            if ((int)attribs.LongLength == 1)
                            {
                                LisDateTimeUsageAttribute dtAttrib = (LisDateTimeUsageAttribute)attribs[0];
                                dateTimeUsage = dtAttrib.DateTimeUsage;
                            }
                            prop.SetValue(this, fRemoveOptionalSubFields(field).LisStringToDateTime(dateTimeUsage), null);
                        }
                        else if (propType.IsEnum)
                        {
                            prop.SetValue(this, fCreateLisEnum(fRemoveOptionalSubFields(field), propType), null);
                        }
                        else if (propType.BaseType == typeof(AbstractLisSubRecord))
                        {
                            prop.SetValue(this, fCreateSubrecord(field, propType), null);
                        }
                        else if (propType.IsArray)
                        {
                            if (!(attrib is LisRecordRemainingFieldsAttribute))
                            {
                                throw new FormatException("The LIS String was not of the correct format.");
                            }
                            prop.SetValue(this, fCreateRemainingFieldsArray(rf, attrib.FieldIndex), null);
                        }
                    }
                    else
                    {
                        prop.SetValue(this, field, null);
                    }
                }
                else
                {
                    prop.SetValue(this, int.Parse(fRemoveOptionalSubFields(field)), null);
                }
            }
        }
示例#2
0
        public virtual string ToLISString()
        {
            bool                     isSubRecord = this is AbstractLisSubRecord;
            char                     sepChar     = ((!isSubRecord) ? LISDelimiters.FieldDelimiter : LISDelimiters.ComponentDelimiter);
            StringBuilder            sb          = new StringBuilder();
            Dictionary <int, string> fieldList   = new Dictionary <int, string>();
            Type                     selfType    = this?.GetType();

            PropertyInfo[] props         = selfType.GetProperties();
            int            maxFieldIndex = int.MinValue;
            int            minFieldIndex = int.MaxValue;
            int            i             = 0;

            PropertyInfo[] array = props;
            string         propString;

            if (array != null)
            {
                for (; i < (int)array.LongLength; i++)
                {
                    PropertyInfo prop    = array[i];
                    object[]     attribs = prop.GetCustomAttributes(typeof(LisRecordFieldAttribute), inherit: false);
                    if ((int)attribs.LongLength <= 0)
                    {
                        continue;
                    }
                    LisRecordFieldAttribute attrib = (LisRecordFieldAttribute)attribs[0];
                    Type propType         = prop.PropertyType;
                    Type nullablePropType = Nullable.GetUnderlyingType(propType);
                    if ((object)nullablePropType != null)
                    {
                        propType = nullablePropType;
                    }
                    propString = null;
                    object propVal = prop.GetValue(this, null);
                    if (propVal != null)
                    {
                        if (propType == typeof(DateTime))
                        {
                            LisDateTimeUsage dateTimeUsage = LisDateTimeUsage.DateTime;
                            attribs = prop.GetCustomAttributes(typeof(LisDateTimeUsageAttribute), inherit: false);
                            if ((int)attribs.LongLength == 1)
                            {
                                LisDateTimeUsageAttribute dtAttrib = (LisDateTimeUsageAttribute)attribs[0];
                                dateTimeUsage = dtAttrib.DateTimeUsage;
                            }
                            propString = ((DateTime)propVal).ToLISDate(dateTimeUsage);
                        }
                        else if (propType.IsEnum)
                        {
                            propString = fGetEnumLisString(propVal);
                        }
                        else if (propType.BaseType == typeof(AbstractLisSubRecord))
                        {
                            propString = (propVal as AbstractLisSubRecord).ToLISString();
                        }
                        else if (propType.IsArray)
                        {
                            if (attrib is LisRecordRemainingFieldsAttribute)
                            {
                                string[] ar = (string[])propVal;
                                propString = string.Join(new string(sepChar, 1), ar);
                            }
                        }
                        else
                        {
                            propString = propVal.ToString();
                        }
                    }
                    if (!string.IsNullOrEmpty(propString))
                    {
                        fieldList.Add(attrib.FieldIndex, propString);
                        if (attrib.FieldIndex > maxFieldIndex)
                        {
                            maxFieldIndex = attrib.FieldIndex;
                        }
                    }
                    if (attrib.FieldIndex < minFieldIndex)
                    {
                        minFieldIndex = attrib.FieldIndex;
                    }
                }
            }
            if (minFieldIndex <= maxFieldIndex)
            {
                int num = maxFieldIndex - 1;
                i = minFieldIndex;
                if (i <= num)
                {
                    num++;
                    do
                    {
                        fieldList.TryGetValue(i, out propString);
                        if (!string.IsNullOrEmpty(propString))
                        {
                            sb.Append(fEscapeString(propString, isSubRecord));
                        }
                        sb.Append(sepChar);
                        i++;
                    }while (i != num);
                }
            }
            fieldList.TryGetValue(maxFieldIndex, out var field);
            if (!string.IsNullOrEmpty(field))
            {
                sb.Append(field);
            }
            if (!isSubRecord)
            {
                sb.Append('\r');
            }
            return(sb.ToString());
        }