Table of Contents

Class SerializationHelper

Namespace
Xunit.Sdk

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

SerializationHelper

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

assembly Assembly

The assembly to get registrations from

warnings List<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

registrations IRegisterXunitSerializerAttribute[]

The serialization registrations

warnings List<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

serializedValue string

The serialized value

Returns

object

The de-serialized object

Deserialize<T>(string)

De-serializes an object.

public T? Deserialize<T>(string serializedValue)

Parameters

serializedValue string

The object's serialized value

Returns

T

The de-serialized object

Type Parameters

T

The type of the object

IsSerializable(object?)

Determines if an object instance is serializable.

public bool IsSerializable(object? value)

Parameters

value object

The object to test for serializability.

Returns

bool

Returns true if 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

value object

The object to test for serializability.

type Type

The type to test for serializability.

Returns

bool

Returns true if 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

value object

The value to be serialized

type Type

The type of the value to be serialized (cannot be null if value is null)

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

assemblyQualifiedTypeName string

The assembly qualified type name ("TypeName,AssemblyName")

Returns

Type

The instance of the Type, if available; null, otherwise.

TypeToSerializedTypeName(Type)

Gets an assembly qualified type name for serialization.

public static string TypeToSerializedTypeName(Type value)

Parameters

value Type

The type to get the name for

Returns

string

A string in "TypeName" format (for mscorlib types) or "TypeName,AssemblyName" format (for all others)