コード例 #1
0
        public bool SerializeComplexRoot(Stream stream, Type type)
        {
            byte arrayRank = 0;
            byte generics  = 0;

            while (type.IsArray)
            {
                arrayRank++;
                type = type.GetElementType();
            }


            ByteArrayKey mainGuid;

            Type[] children = null;
            if (type.IsConstructedGenericType)
            {
                if (!TypesLookup.TryGetValue(type.GetGenericTypeDefinition(), out mainGuid))
                {
                    return(false);
                }
                children  = type.GetGenericArguments();
                generics += checked ((byte)children.Length);
            }
            else
            {
                if (!TypesLookup.TryGetValue(type, out mainGuid))
                {
                    return(false);
                }
            }


            if (children != null)
            {
                foreach (var child in children)
                {
                    if (!SerializeComplex(null, child))
                    {
                        return(false);
                    }
                }
            }
            stream.WriteByte(arrayRank);
            stream.WriteByte(generics);
            stream.Write(mainGuid.Bytes, 0, mainGuid.Bytes.Length);

            if (children != null)
            {
                foreach (var child in children)
                {
                    if (!SerializeComplex(stream, child))
                    {
                        throw new InvalidOperationException();
                    }
                }
            }

            return(true);
        }
コード例 #2
0
        public override bool TryWrite(Stream stream, Type type)
        {
            if (!stream.CanWrite)
            {
                throw new ArgumentException(nameof(stream));
            }
            if (!TypesLookup.TryGetValue(type, out var guid))
            {
                return(false);
            }
            stream.WriteByte(Signature);
            stream.Write(guid.Bytes, 0, 16);

            return(true);
        }