Class SerializationHelper
Serializes and de-serializes objects. It supports a limited set of built-in types, as well as anything which implements IXunitSerializable. Custom serializers can implement IXunitSerializer and register by decorating the test assembly with RegisterXunitSerializerAttribute.
public class SerializationHelper
- Inheritance
-
SerializationHelper
- Inherited Members
- Extension Methods
Constructors
SerializationHelper()
Initializes a new instance of the SerializationHelper class.
protected SerializationHelper()
Properties
Instance
Gets the singleton instance of SerializationHelper.
public static SerializationHelper Instance { get; }
Property Value
Methods
AddRegisteredSerializers(Assembly, List<string>?)
Add serializers that have been registered in the given assembly.
public void AddRegisteredSerializers(Assembly assembly, List<string>? warnings = null)
Parameters
assemblyAssemblyThe assembly to get registrations from
warningsList<string>An optional collection to receive warnings generated during the registration
Remarks
The warnings collection will include warnings in the following circumstances:
- When the serializer type that does not implement IXunitSerializer
- When the registration contains no support types to serialize
- When a supported type to serialize is duplicated with another serializer
- When a supported type is covered by a built-in serializer
- An exception is thrown while creating the serializer
AddSerializers(IRegisterXunitSerializerAttribute[], List<string>?)
Add serializers to the supported serializer list.
protected void AddSerializers(IRegisterXunitSerializerAttribute[] registrations, List<string>? warnings = null)
Parameters
registrationsIRegisterXunitSerializerAttribute[]The serialization registrations
warningsList<string>An optional collection to receive warnings generated during the registration
Remarks
The warnings collection will include warnings in the following circumstances:
- When the serializer type that does not implement IXunitSerializer
- When the registration contains no support types to serialize
- When a supported type to serialize is duplicated with another serializer
- When a supported type is covered by a built-in serializer
- An exception is thrown while creating the serializer
Deserialize(string)
De-serializes an object.
public object? Deserialize(string serializedValue)
Parameters
serializedValuestringThe serialized value
Returns
- object
The de-serialized object
Deserialize<T>(string)
De-serializes an object.
public T? Deserialize<T>(string serializedValue)
Parameters
serializedValuestringThe object's serialized value
Returns
- T
The de-serialized object
Type Parameters
TThe type of the object
IsSerializable(object?)
Determines if an object instance is serializable.
public bool IsSerializable(object? value)
Parameters
valueobjectThe object to test for serializability.
Returns
- bool
Returns
trueif the object can be serialized;false, otherwise.
Remarks
As null values always return true, even if the underlying type (which is unknown)
might not be serializable, it's better to test via IsSerializable(object?, Type?)
whenever possible.
IsSerializable(object?, Type?)
Determines if a given type supports serialization.
public bool IsSerializable(object? value, Type? type)
Parameters
valueobjectThe object to test for serializability.
typeTypeThe type to test for serializability.
Returns
- bool
Returns
trueif objects of the given type can be serialized;false, otherwise.
Serialize(object?, Type?)
Serializes an object.
public string Serialize(object? value, Type? type = null)
Parameters
valueobjectThe value to be serialized
typeTypeThe type of the value to be serialized (cannot be
nullifvalueisnull)
Returns
- string
The serialized object
SerializedTypeNameToType(string)
Converts a type name (in "TypeName" format for mscorlib types, or "TypeName,AssemblyName" format for all others) into a Type object.
public static Type? SerializedTypeNameToType(string assemblyQualifiedTypeName)
Parameters
assemblyQualifiedTypeNamestringThe assembly qualified type name ("TypeName,AssemblyName")
Returns
TypeToSerializedTypeName(Type)
Gets an assembly qualified type name for serialization.
public static string TypeToSerializedTypeName(Type value)
Parameters
valueTypeThe type to get the name for
Returns
- string
A string in "TypeName" format (for mscorlib types) or "TypeName,AssemblyName" format (for all others)