BinarySerializerDeserializeByReaderT(BinaryReader, BinarySerializationOptions, Type) Method
Deserializes the content of a serialization stream wrapped by the specified reader from its current position
into an instance of T.
Namespace: KGySoft.Serialization.BinaryAssembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
public static T DeserializeByReader<T>(
BinaryReader reader,
BinarySerializationOptions options,
params Type[]? expectedCustomTypes
)
Public Shared Function DeserializeByReader(Of T) (
reader As BinaryReader,
options As BinarySerializationOptions,
ParamArray expectedCustomTypes As Type()
) As T
public:
generic<typename T>
static T DeserializeByReader(
BinaryReader^ reader,
BinarySerializationOptions options,
... array<Type^>^ expectedCustomTypes
)
static member DeserializeByReader :
reader : BinaryReader *
options : BinarySerializationOptions *
expectedCustomTypes : Type[] -> 'T
- reader BinaryReader
- The reader that wraps the stream containing the serialized data. The reader will remain open after deserialization.
- options BinarySerializationOptions
- Options of the deserialization.
- expectedCustomTypes Type
- The types that are expected to present in the serialization stream by name.
If SafeMode is not enabled in options
or the stream does not contain any types by name, then this parameter is optional.
- T
- The expected type of the result.
TThe deserialized instance of
T.
If data was serialized by
Serialize or
SerializeToStream methods, then
reader must use UTF-8 encoding to get the correct result. If data was serialized by
the
SerializeByWriter method, then you must use the same encoding as was used there.
expectedCustomTypes must be specified if SafeMode is enabled in options
and the serialization stream contains types encoded by their names. Natively supported types are not needed to be included
unless the original object was serialized with the ForceRecursiveSerializationOfSupportedTypes option enabled.
T is allowed to be an interface or abstract type but if it's different from the actual type of the result,
then the actual type also might needed to be included in expectedCustomTypes.
You can specify expectedCustomTypes even if SafeMode is not enabled in options
as it may improve the performance of type resolving and can help avoiding possible ambiguities if types were not serialized with full assembly identity
(e.g. if OmitAssemblyQualifiedNames was enabled on serialization).
If a type in expectedCustomTypes has a different assembly identity in the deserialization stream, and
it is not indicated by a TypeForwardedFromAttribute declared on the type, then you should instantiate a BinarySerializationFormatter class
manually and set its Binder property to a ForwardedTypesSerializationBinder instance
to specify the expected types.
For arrays it is enough to specify the element type and for generic types you can specify the
natively not supported generic type definition and generic type arguments separately.
If expectedCustomTypes contains constructed generic types, then the generic type definition and
the type arguments will be treated as expected types in any combination.
If
T is a custom type using default recursive serialization, and it contains further custom types
you can use the
ExtractExpectedTypes overloads
to auto-detect the expected types.