public static string ToLISDate(this DateTime dateTime, LisDateTimeUsage lisDateTimeUsage)
        {
            switch (lisDateTimeUsage)
            {
            default:
            {
                if (lisDateTimeUsage == LisDateTimeUsage.Date)
                {
                    goto case LisDateTimeUsage.Date;
                }
                if (lisDateTimeUsage == LisDateTimeUsage.DateTime)
                {
                    goto case LisDateTimeUsage.DateTime;
                }
                string Result = default(string);
                if (lisDateTimeUsage != LisDateTimeUsage.Time)
                {
                    return(Result);
                }
                goto case LisDateTimeUsage.Time;
            }

            case LisDateTimeUsage.Date:
                return(dateTime.Year.ToString("D4") + dateTime.Month.ToString("D2") + dateTime.Day.ToString("D2"));

            case LisDateTimeUsage.DateTime:
                return(dateTime.Year.ToString("D4") + dateTime.Month.ToString("D2") + dateTime.Day.ToString("D2") + dateTime.Hour.ToString("D2") + dateTime.Minute.ToString("D2") + dateTime.Second.ToString("D2"));

            case LisDateTimeUsage.Time:
                return(dateTime.Hour.ToString("D2") + dateTime.Minute.ToString("D2") + dateTime.Second.ToString("D2"));
            }
        }
        public static DateTime LisStringToDateTime(this string lisString, LisDateTimeUsage lisDateTimeUsage)
        {
            switch (lisDateTimeUsage)
            {
            default:
            {
                if (lisDateTimeUsage == LisDateTimeUsage.Date)
                {
                    goto case LisDateTimeUsage.Date;
                }
                if (lisDateTimeUsage == LisDateTimeUsage.DateTime)
                {
                    goto case LisDateTimeUsage.DateTime;
                }
                DateTime Result = default(DateTime);
                if (lisDateTimeUsage != LisDateTimeUsage.Time)
                {
                    return(Result);
                }
                goto case LisDateTimeUsage.Time;
            }

            case LisDateTimeUsage.Date:
                return(DateTime.ParseExact(lisString, "yyyyMMdd", CultureInfo.InvariantCulture));

            case LisDateTimeUsage.DateTime:
                return(DateTime.ParseExact(lisString, "yyyyMMddHHmmss", CultureInfo.InvariantCulture));

            case LisDateTimeUsage.Time:
                return(DateTime.ParseExact(lisString, "HHmmss", CultureInfo.InvariantCulture));
            }
        }
 public LisDateTimeUsageAttribute(LisDateTimeUsage dateTimeUsage)
 {
     DateTimeUsage = dateTimeUsage;
 }
Exemple #4
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);
                }
            }
        }
Exemple #5
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());
        }