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.
public static T DeserializeFromStream<T>(
Stream stream,
BinarySerializationOptions options,
params Type[]? expectedCustomTypes
)
Public Shared Function DeserializeFromStream(Of T) (
stream As Stream,
options As BinarySerializationOptions,
ParamArray expectedCustomTypes As Type()
) As T
public:
generic<typename T>
static T DeserializeFromStream(
Stream^ stream,
BinarySerializationOptions options,
... array<Type^>^ expectedCustomTypes
)
static member DeserializeFromStream :
stream : Stream *
options : BinarySerializationOptions *
expectedCustomTypes : Type[] -> 'T
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.