示例#1
0
        public virtual int encodeSequencePreamble(object obj, PropertyInfo[] fields, System.IO.Stream stream, ElementInfo elementInfo)
        {
            int         resultBitSize = 0;
            ElementInfo info          = new ElementInfo();
            int         fieldIdx      = 0;

            foreach (PropertyInfo field in fields)
            {
                if (elementInfo.hasPreparedInfo())
                {
                    info.PreparedInfo = elementInfo.PreparedInfo.getPropertyMetadata(fieldIdx);
                }

                if (CoderUtils.isOptionalField(field, info))
                {
                    object invokeObjResult = invokeGetterMethodForField(field, obj, info);
                    if (invokeObjResult == null)
                    {
                        ((BitArrayOutputStream)stream).writeBit(false);
                    }
                    else if (CoderUtils.isDefaultField(field, info))
                    {
                        object newSequenceInstance = Activator.CreateInstance(obj.GetType());
                        CoderUtils.initDefaultValues(newSequenceInstance);
                        object defaultFieldValue = invokeGetterMethodForField(field, newSequenceInstance, info);
                        ((BitArrayOutputStream)stream).writeBit(!CoderUtils.AreEqual(defaultFieldValue, invokeObjResult));
                    }
                    else
                    {
                        ((BitArrayOutputStream)stream).writeBit(true);
                    }
                    resultBitSize += 1;
                }
                fieldIdx++;
            }
            doAlign(stream);
            return((resultBitSize / 8) + (resultBitSize % 8 > 0?1:0));
        }