コード例 #1
0
        public void Write <T>(SerializationCtx ctx, UnsafeWriter writer, T value)
        {
#if !NET35
            myBackgroundRegistrar.WaitForEmpty();
#endif

            if (value == null)
            {
                // ReSharper disable once ImpureMethodCallOnReadonlyValueField
                RdId.Nil.Write(writer);
                return;
            }

            RdId typeId;
            var  type = value.GetType();
            if (!myTypeMapping.TryGetValue(type, out typeId))
            {
                myPolymorphicCatalog?.TryDiscoverRegister(type, this);
                if (!myTypeMapping.TryGetValue(type, out typeId))
                {
                    throw new KeyNotFoundException($"Type {type.FullName} have not registered");
                }
            }
            typeId.Write(writer);

            // Don't dispose this cookie, otherwise it will delete all written data
            var cookie = new UnsafeWriter.Cookie(writer);
            writer.Write(0);

            var writerDelegate = myWriters[typeId];
            writerDelegate(ctx, writer, value);

            cookie.WriteIntLengthToCookieStart();
        }
コード例 #2
0
ファイル: SerializersEx.cs プロジェクト: yvvan/rd
        public static void WriteEnumerable <T>(this UnsafeWriter writer, CtxWriteDelegate <T> itemWriter, SerializationCtx ctx, IEnumerable <T> value)
        {
            if (value == null)
            {
                writer.Write(-1);
                return;
            }

            // // Don't dispose this cookie, otherwise it will delete all written data
            var cookie = new UnsafeWriter.Cookie(writer);

            cookie.Writer.Write(-1); // length
            int i = 0;

            foreach (var item in value)
            {
                ++i;
                itemWriter(ctx, writer, item);
            }

            cookie.WriteIntLengthToCookieStart(i);
        }