public byte[] ToBytes()
        {
            //统一编码消息头 MH

            CoreMessageHeader msgHeader = new CoreMessageHeader();
            msgHeader.MH_MESSAGE_LENGTH = CoreMessageHeader.TOTAL_WIDTH + RQ_TOTAL_WIDTH;
            msgHeader.MH_LAST_FLAG = MessageHeaderLastFlag;
            byte[] buffer = new byte[msgHeader.MH_MESSAGE_LENGTH];
            Array.Copy(msgHeader.ToBytes(), buffer, CoreMessageHeader.TOTAL_WIDTH);

            byte[] rqtotal = new byte[RQ_TOTAL_WIDTH];
            CoreDataBlockHeader dbhdr1 = new CoreDataBlockHeader();
            dbhdr1.DBH_DB_LENGTH = CoreDataBlockHeader.TOTAL_WIDTH + RQHDR_MsgHandler.TOTAL_WIDTH;
            dbhdr1.DBH_DB_ID = "@RQHDR";
            Array.Copy(dbhdr1.ToBytes(), rqtotal, CoreDataBlockHeader.TOTAL_WIDTH);
            Array.Copy(RQhdrHandler.ToBytes(), 0, rqtotal, CoreDataBlockHeader.TOTAL_WIDTH, RQHDR_MsgHandler.TOTAL_WIDTH);

            if (GetRQDTLLen() > 0)
            {
                CoreDataBlockHeader dbhdr2 = new CoreDataBlockHeader();
                dbhdr2.DBH_DB_LENGTH = (UInt32)(CoreDataBlockHeader.TOTAL_WIDTH + GetRQDTLLen());
                dbhdr2.DBH_DB_ID = "@RQDTL";
                Array.Copy(dbhdr2.ToBytes(), 0, rqtotal, CoreDataBlockHeader.TOTAL_WIDTH + RQHDR_MsgHandler.TOTAL_WIDTH, CoreDataBlockHeader.TOTAL_WIDTH);
                rqtotal = RQDTL_ToBytes(rqtotal);
            }
            Array.Copy(rqtotal, 0, buffer, CoreMessageHeader.TOTAL_WIDTH, RQ_TOTAL_WIDTH);
            return buffer;
        }
        public new byte[] ToBytes()
        {

            if (IBDATACollection == null || IBDATACollection.Count == 0)
            {
                base.MessageHeaderLastFlag = true;
                return base.ToBytes();
            }
            else
            {
                base.MessageHeaderLastFlag = false;
                byte[] regularBytes = base.ToBytes();
                int index = 0;
                
                int ibdataLength = (from ibdata in IBDATACollection
                                   where ibdata != null && ibdata.TOTAL_WIDTH > 0
                                    select ibdata.TOTAL_WIDTH).Sum() + IBDATACollection.Count * (CoreDataBlockHeader.TOTAL_WIDTH + CoreMessageHeader.TOTAL_WIDTH);
                byte [] result = new byte[ibdataLength + regularBytes.Length];
                Array.Copy(regularBytes, result, regularBytes.Length);
                int offset = regularBytes.Length;
                foreach (var ibdata in IBDATACollection)
                {
                    CoreMessageHeader msgHeader = new CoreMessageHeader();
                    msgHeader.MH_MESSAGE_LENGTH = (uint)(CoreMessageHeader.TOTAL_WIDTH + CoreDataBlockHeader.TOTAL_WIDTH + ibdata.TOTAL_WIDTH);
                    msgHeader.MH_LAST_FLAG = (++index == IBDATACollection.Count) ? true : false;
                    Array.Copy(msgHeader.ToBytes(), 0, result, offset,  CoreMessageHeader.TOTAL_WIDTH);
                    offset += CoreMessageHeader.TOTAL_WIDTH;
                    
                    CoreDataBlockHeader dbhdr = new CoreDataBlockHeader();
                    dbhdr.DBH_DB_LENGTH = (uint)(CoreDataBlockHeader.TOTAL_WIDTH + ibdata.TOTAL_WIDTH);
                    dbhdr.DBH_DB_ID = "@IBDATA";
                    Array.Copy(dbhdr.ToBytes(), 0, result, offset, CoreDataBlockHeader.TOTAL_WIDTH);
                    offset += CoreDataBlockHeader.TOTAL_WIDTH;
                    Array.Copy(ibdata.ToBytes(), 0, result, offset, ibdata.TOTAL_WIDTH);
                    offset += ibdata.TOTAL_WIDTH;
                }

                return result;                
            }
        }
        public new object FromBytes(byte[] messagebytes)
        {
            if (messagebytes != null && messagebytes.Length > 0)
            {
                bool lastFlag = false;
                int len = messagebytes.Length;
                int offset = 0;
                while (messagebytes.Length > 0 && !lastFlag)
                {
                    byte[] buffer = new byte[CoreMessageHeader.TOTAL_WIDTH];
                    if (messagebytes.Length >= buffer.Length)
                    {
                        Array.Copy(messagebytes, buffer, buffer.Length);
                    }
                    else
                    {
                        return null;
                    }
                    CoreMessageHeader msgHeader = new CoreMessageHeader();
                    msgHeader.FromBytes(buffer);
                    lastFlag = msgHeader.MH_LAST_FLAG;

                    int mbLen = (int)(msgHeader.MH_MESSAGE_LENGTH - CoreMessageHeader.TOTAL_WIDTH);
                    buffer = new byte[mbLen];

                    Array.Copy(messagebytes, CoreMessageHeader.TOTAL_WIDTH, buffer, 0, mbLen);

                    offset = (int)msgHeader.MH_MESSAGE_LENGTH;
                    //len -= offset;

                    DetailFromBytes(buffer);
                    messagebytes = CommonDataHelper.SubBytes(messagebytes, offset, messagebytes.Length - offset);
                }
            }
            
            return this;
        }
        public static BizMsgDataBase BytesToCoreMsgData(byte[] recbytes, IMessageRespHandler dataref)
        {
            if (recbytes != null && recbytes.Length > 0)
            {
                byte[] buffer = new byte[CoreMessageHeader.TOTAL_WIDTH];
                if (recbytes.Length >= buffer.Length)
                {
                    Array.Copy(recbytes, buffer, buffer.Length);
                }
                else
                {
                    return null;
                }
                CoreMessageHeader msgHeader = new CoreMessageHeader();
                msgHeader.FromBytes(buffer);

                UInt32 mbLen = msgHeader.MH_MESSAGE_LENGTH - CoreMessageHeader.TOTAL_WIDTH;
                buffer = new byte[mbLen];

                //Assembly asm = Assembly.GetExecutingAssembly();
                //object dataobj = asm.CreateInstance(type.ToString(), false);
                //IMessageRespHandler bizMsgData = dataref as IMessageRespHandler;
                if (dataref != null)
                {
                    Array.Copy(recbytes, CoreMessageHeader.TOTAL_WIDTH, buffer, 0, mbLen);
                    dataref.FromBytes(buffer);
                }
                return dataref as CoreBizMsgDataBase;
            }
            else
            {
                return null;
            }
        }
        public object FromBytes(byte[] messagebytes)
        {
            // MessageHeader
            byte[] buffer = new byte[CoreMessageHeader.TOTAL_WIDTH];
            if (messagebytes.Length >= buffer.Length)
            {
                Array.Copy(messagebytes, buffer, buffer.Length);
            }
            else
            {
                return null;
            }
            CoreMessageHeader msgHeader = new CoreMessageHeader();
            msgHeader.FromBytes(buffer);

            UInt32 mbLen = msgHeader.MH_MESSAGE_LENGTH - CoreMessageHeader.TOTAL_WIDTH;
            messagebytes = CommonDataHelper.SubBytes(messagebytes, (int)CoreMessageHeader.TOTAL_WIDTH, (int)mbLen);

            // MessageBody
            int len = messagebytes.Length;
            int offset = 0;

            while (len > 0)
            {
                buffer = new byte[CoreDataBlockHeader.TOTAL_WIDTH];
                Array.Copy(messagebytes, offset, buffer, 0, CoreDataBlockHeader.TOTAL_WIDTH);
                CoreDataBlockHeader dbhdr1 = new CoreDataBlockHeader();
                dbhdr1 = (CoreDataBlockHeader)dbhdr1.FromBytes(buffer);
                len -= CoreDataBlockHeader.TOTAL_WIDTH;
                offset += CoreDataBlockHeader.TOTAL_WIDTH;
                if (len > 0)
                {
                    switch (dbhdr1.DBH_DB_ID.Trim())
                    {
                        case "@RPHDR":
                            buffer = new byte[RPHDR_MsgHandler.TOTAL_WIDTH];
                            Array.Copy(messagebytes, offset, buffer, 0, RPHDR_MsgHandler.TOTAL_WIDTH);
                            RPhdrHandler = (RPHDR_MsgHandler)RPhdrHandler.FromBytes(buffer);
                            len -= RPHDR_MsgHandler.TOTAL_WIDTH;
                            offset += RPHDR_MsgHandler.TOTAL_WIDTH;
                            break;

                        case "@ODATA":
                            //buffer = new byte[GetODATALen()];
                            UInt16 odataLen = (UInt16)(dbhdr1.DBH_DB_LENGTH - CoreDataBlockHeader.TOTAL_WIDTH);
                            buffer = new byte[odataLen];
                            Array.Copy(messagebytes, offset, buffer, 0, odataLen);
                            //Array.Copy(messagebytes, offset, buffer, 0, GetODATALen());
                            //OData = (RetrieveCstmODATA)OData.FromBytes(buffer);
                            ODATA_FromBytes(buffer);
                            len -= odataLen;
                            offset += odataLen;
                            //len -= GetODATALen();
                            //offset += GetODATALen();
                            break;
                        //case "@OBDATA":
                        //    UInt32 obdataLen = dbhdr1.DBH_DB_LENGTH - CoreDataBlockHeader.TOTAL_WIDTH;
                        //    buffer = new byte[obdataLen];
                        //    Array.Copy(messagebytes, offset, buffer, 0, obdataLen);
                        //    OBDATA_FromBytes(buffer);
                        //    len -= (Int32)obdataLen;
                        //    offset += (Int32)obdataLen;
                        //    break;

                        case "@OMSG":
                            UInt32 omsgLen = dbhdr1.DBH_DB_LENGTH - CoreDataBlockHeader.TOTAL_WIDTH;
                            buffer = new byte[omsgLen];
                            Array.Copy(messagebytes, offset, buffer, 0, omsgLen);
                            OmsgHandler = (OMSG_MsgHandler)OmsgHandler.FromBytes(buffer);
                            len -= (int)omsgLen;
                            offset += (int)omsgLen;
                            break;

                        case "@SYSERR":
                            int syserrLen = (int)dbhdr1.DBH_DB_LENGTH - CoreDataBlockHeader.TOTAL_WIDTH;
                            buffer = new byte[syserrLen];
                            //ms.Read(buffer, offset, (int)syserrLen);
                            Array.Copy(messagebytes, offset, buffer, 0, syserrLen);
                            SyserrHandler = (SYSERR_MsgHandler)SyserrHandler.FromBytes(buffer);
                            len -= (int)syserrLen;
                            offset += (int)syserrLen;
                            break;
                        default:
                            len--;
                            break;

                    }
                }
            }

            return this;
        }
        public static byte[] CoreMsgDataToBytes(IMessageReqHandler data)
        {
            if (data != null)
            {
                //统一编码消息头 MH

                CoreMessageHeader msgHeader = new CoreMessageHeader();
                msgHeader.MH_MESSAGE_LENGTH = CoreMessageHeader.TOTAL_WIDTH + ((CoreBizMsgDataBase)data).RQ_TOTAL_WIDTH;
                byte[] buffer = new byte[msgHeader.MH_MESSAGE_LENGTH];
                Array.Copy(msgHeader.ToBytes(), buffer, CoreMessageHeader.TOTAL_WIDTH);

                Array.Copy(data.ToBytes(), 0, buffer, CoreMessageHeader.TOTAL_WIDTH, ((CoreBizMsgDataBase)data).RQ_TOTAL_WIDTH);
                return buffer;
            }
            return null;
        }