Class SerializationContext
Provides a context for storing and retrieving key-value pairs during serialization operations.
public class SerializationContext
- Inheritance
-
SerializationContext
- Inherited Members
Remarks
Use this class to pass additional contextual information between serialization components. The context is typically used to share data that may influence how objects are serialized or deserialized. Keys are case-sensitive. This class is not thread-safe; synchronize access if used concurrently from multiple threads.
Properties
ContextData
Gets a collection of key-value pairs that provide additional contextual information associated with the current instance.
public Dictionary<string, object> ContextData { get; }
Property Value
Remarks
Use this property to store or retrieve custom data relevant to the context in which the instance is used. The dictionary is initialized and never null, but may be empty if no context data has been added.
Methods
GetValue<T>(string, T)
Retrieves the value associated with the specified key, or returns a default value if the key is not found or cannot be cast to the specified type.
public T GetValue<T>(string key, T defaultValue = default)
Parameters
keystringThe key whose associated value is to be retrieved.
defaultValueTThe value to return if the key does not exist or the value cannot be cast to type T.
Returns
- T
The value associated with the specified key, cast to type T, if found and of the correct type; otherwise, the specified default value.
Type Parameters
TThe type of the value to retrieve.
Remarks
If the value associated with the key exists but is not of type T, the default value is returned instead of throwing an exception.
SetValue<T>(string, T)
Sets the value associated with the specified key in the context data.
public void SetValue<T>(string key, T value)
Parameters
keystringThe key with which the value will be associated. Cannot be null.
valueTThe value to associate with the specified key.
Type Parameters
TThe type of the value to store.