/// <summary> /// Deserialize object of unknown types from in input stream. /// </summary> /// <param name="source">The input stream.</param> /// <param name="style">The prefix style used to encode the lengths.</param> /// <param name="typeReader">The caller must provide a mechanism to resolve a Type from /// the tags encountered in the stream. If the delegate returns null, then the instance /// is skipped - otherwise, the object is deserialized according to type.</param> /// <param name="item">The deserialized instance, or null if the stream terminated.</param> /// <returns>True if an object was idenfified; false if the stream terminated. Note /// that unexpected types are skipped.</returns> public static bool TryDeserializeWithLengthPrefix(Stream source, PrefixStyle style, Getter <int, Type> typeReader, out object item) { uint len; Type itemType = null; Getter <int, bool> processField = null; if (typeReader != null) { processField = delegate(int checkTag) { itemType = typeReader(checkTag); return(itemType != null); } } ; if (!Serializer.TryReadPrefixLength(source, style, 1, out len, processField)) { item = null; return(false); } if (len == uint.MaxValue) { item = NonGeneric.Deserialize(itemType, source); } else { using (SubStream subStream = new SubStream(source, len, false)) { item = NonGeneric.Deserialize(itemType, subStream); } } return(true); }