BinarySerializerDeserializeFromStreamT(Stream, BinarySerializationOptions, Type) Method

Deserializes the content of the specified serialization stream from its current position into an instance of T.

Definition

Namespace: KGySoft.Serialization.Binary
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
C#
public static T DeserializeFromStream<T>(
	Stream stream,
	BinarySerializationOptions options,
	params Type[]? expectedCustomTypes
)

Parameters

stream  Stream
The Stream containing the serialized data. The stream must support reading and 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 stream does not contain any types by name, then this parameter is optional.

Type Parameters

T
The expected type of the result.

Return Value

T
The deserialized instance of T.

Remarks

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.

  Tip

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.

See Also