internal static void Write(object e, int tag, TarsOutputStream jos)
        {
            TarsStructInfo info = TarsHelper.GetStructInfo(e.GetType());

            if (info == null)
            {
                throw new TarsDecodeException("the class type[" + e.GetType().FullName + "] no attribute Struct");
            }
            jos.WriteHead(TarsStructBase.STRUCT_BEGIN, tag);
            List <TarsStructPropertyInfo> propertysList = info.PropertyList;

            foreach (var propertyInfo in propertysList)
            {
                Object value = propertyInfo.PropertyAccessor.GetValue(e);
                if (value == null && propertyInfo.IsRequire)
                {
                    throw new TarsEncodeException(propertyInfo.Name + " is require tag ,order=" + propertyInfo.Order);
                }
                if (value != null)
                {
                    jos.Write(value, propertyInfo.Order);
                }
            }
            jos.WriteHead(TarsStructBase.STRUCT_END, 0);
        }
Exemple #2
0
        internal static object Read(Type type, int tag, bool isRequire, TarsInputStream jis)
        {
            TarsStructInfo info = TarsHelper.GetStructInfo(type);

            if (info == null)
            {
                throw new TarsDecodeException("the class type[" + type.FullName + "] no attribute Struct");
            }
            if (jis.SkipToTag(tag))
            {
                HeadData hd = new HeadData();
                jis.ReadHead(hd);
                if (hd.Type != TarsStructBase.STRUCT_BEGIN)
                {
                    throw new TarsDecodeException("type mismatch.");
                }
                Object result = info.ConstructorInvoker.Invoke();
                List <TarsStructPropertyInfo> propertysList = info.PropertyList;
                foreach (var propertyInfo in propertysList)
                {
                    Object value = jis.Read(propertyInfo.Type, propertyInfo.Order, propertyInfo.IsRequire);
                    propertyInfo.PropertyAccessor.SetValue(result, value);
                }
                jis.SkipToStructEnd();
                return(result);
            }
            else if (isRequire)
            {
                throw new TarsDecodeException("require field not exist.");
            }
            return(null);
        }