예제 #1
0
        public QuickSerializer(
            Type recordType, 
            DbfVersion version, 
            List<ColumnDefinition> columns, 
            int recordWidth,
            bool ignoreMissingFields, 
            bool setOffset)
        {
            mVersion = version;
            if (columns == null)
            {
                columns = new List<ColumnDefinition>();
                mCreateColumns = true;
            }
            mColumns = columns;
            mIgnoreMissingFields = ignoreMissingFields;

            mRecordWidth = 0; // dbf use a character to specify deleted record
            mIsFixedRecordWidth = true;

            var readMethod = new DynamicMethod("__readRecord",
                        null,
                        new Type[] { typeof(IHasEncoding), typeof(Byte[]), typeof(object) },
                        recordType,
                        true);

            mIlRead = readMethod.GetILGenerator();

            var writeMethod = new DynamicMethod("__writeRecord",
                        null,
                        new Type[] { typeof(IHasEncoding), typeof(Byte[]), typeof(object) },
                        recordType,
                        true);

            mIlWrite = writeMethod.GetILGenerator();

            var compareMethod = new DynamicMethod("__compareRecord",
                        typeof(int),
                        new Type[] { typeof(IHasEncoding), typeof(object), typeof(object) },
                        recordType,
                        true);

            mIlCompare = compareMethod.GetILGenerator();

            EnumerateFields(recordType);

            int currentOffset = 0;
            int columnIndex = 0;
            foreach(var cd in mColumns)
            {
                if (setOffset)
                {
                    cd.mOffset = currentOffset;
                    currentOffset += cd.mWidth;
                }
                cd.ColumnIndex = columnIndex++;
                EmitColumnCode(cd);
            }
            mRecordWidth = currentOffset;

            mIlRead.Emit(OpCodes.Ret);
            mIlWrite.Emit(OpCodes.Ret);

            mIlCompare.Emit(OpCodes.Ldc_I4_0); // if not return yet, we can say that the records are equal
            mIlCompare.Emit(OpCodes.Ret);

            mQuickReadMethod = (QuickReadDelegate)readMethod.CreateDelegate(typeof(QuickReadDelegate));
            mQuickWriteMethod = (QuickWriteDelegate)writeMethod.CreateDelegate(typeof(QuickWriteDelegate));
            mQuickCompareMethod = (QuickCompareDelegate)compareMethod.CreateDelegate(typeof(QuickCompareDelegate));

            if (recordWidth > 0) mRecordWidth = recordWidth;
        }
예제 #2
0
        public QuickSerializer(
            Type recordType,
            DbfVersion version,
            List <ColumnDefinition> columns,
            int recordWidth,
            bool ignoreMissingFields,
            bool setOffset)
        {
            mVersion = version;
            if (columns == null)
            {
                columns        = new List <ColumnDefinition>();
                mCreateColumns = true;
            }
            mColumns             = columns;
            mIgnoreMissingFields = ignoreMissingFields;

            mRecordWidth        = 0; // dbf use a character to specify deleted record
            mIsFixedRecordWidth = true;

            var readMethod = new DynamicMethod("__readRecord",
                                               null,
                                               new Type[] { typeof(IHasEncoding), typeof(Byte[]), typeof(object) },
                                               recordType,
                                               true);

            mIlRead = readMethod.GetILGenerator();

            var writeMethod = new DynamicMethod("__writeRecord",
                                                null,
                                                new Type[] { typeof(IHasEncoding), typeof(Byte[]), typeof(object) },
                                                recordType,
                                                true);

            mIlWrite = writeMethod.GetILGenerator();

            var compareMethod = new DynamicMethod("__compareRecord",
                                                  typeof(int),
                                                  new Type[] { typeof(IHasEncoding), typeof(object), typeof(object) },
                                                  recordType,
                                                  true);

            mIlCompare = compareMethod.GetILGenerator();

            EnumerateFields(recordType);

            int currentOffset = 0;
            int columnIndex   = 0;

            foreach (var cd in mColumns)
            {
                if (setOffset)
                {
                    cd.mOffset     = currentOffset;
                    currentOffset += cd.mWidth;
                }
                cd.ColumnIndex = columnIndex++;
                EmitColumnCode(cd);
            }
            mRecordWidth = currentOffset;

            mIlRead.Emit(OpCodes.Ret);
            mIlWrite.Emit(OpCodes.Ret);

            mIlCompare.Emit(OpCodes.Ldc_I4_0); // if not return yet, we can say that the records are equal
            mIlCompare.Emit(OpCodes.Ret);

            mQuickReadMethod    = (QuickReadDelegate)readMethod.CreateDelegate(typeof(QuickReadDelegate));
            mQuickWriteMethod   = (QuickWriteDelegate)writeMethod.CreateDelegate(typeof(QuickWriteDelegate));
            mQuickCompareMethod = (QuickCompareDelegate)compareMethod.CreateDelegate(typeof(QuickCompareDelegate));

            if (recordWidth > 0)
            {
                mRecordWidth = recordWidth;
            }
        }