Class PersistableHelper
- Namespace
- Ecng.Serialization
- Assembly
- Ecng.Serialization.dll
Provides helper methods for persisting, cloning, loading and saving objects.
public static class PersistableHelper
- Inheritance
-
PersistableHelper
- Inherited Members
Methods
ApplyAsync<T>(T, T, CancellationToken)
Asynchronously applies the state from the clone to the target asynchronous persistable object.
public static ValueTask ApplyAsync<T>(this T obj, T clone, CancellationToken cancellationToken = default) where T : IAsyncPersistable
Parameters
objTThe target object.
cloneTThe object from which to copy the state.
cancellationTokenCancellationTokenA token for cancellation.
Returns
- ValueTask
A ValueTask representing the asynchronous operation.
Type Parameters
TThe type of the asynchronous persistable object.
Apply<T>(T, T)
Applies the state from the clone to the target persistable object.
public static void Apply<T>(this T obj, T clone) where T : IPersistable
Parameters
objTThe target object.
cloneTThe object from which to copy the state.
Type Parameters
TThe type of the persistable object.
CloneAsync<T>(T, CancellationToken)
Asynchronously clones the specified asynchronous persistable object.
public static ValueTask<T> CloneAsync<T>(this T obj, CancellationToken cancellationToken = default) where T : IAsyncPersistable
Parameters
objTThe object to clone.
cancellationTokenCancellationTokenA token for cancellation.
Returns
- ValueTask<T>
A ValueTask with the cloned object.
Type Parameters
TThe type of the asynchronous persistable object.
Clone<T>(T)
Clones the specified persistable object.
public static T Clone<T>(this T obj) where T : IPersistable
Parameters
objTThe object to clone.
Returns
- T
A clone of the object.
Type Parameters
TThe type of the persistable object.
ForceLoad<T>(T, SettingsStorage)
Forces the persistable object to load its state from the given settings storage.
public static void ForceLoad<T>(this T t, SettingsStorage storage) where T : IPersistable
Parameters
tTThe target object.
storageSettingsStorageThe settings storage to load from.
Type Parameters
TThe type of the persistable object.
FromStorage(SettingsStorage)
Converts the settings storage back to an object.
public static object FromStorage(this SettingsStorage storage)
Parameters
storageSettingsStorageThe settings storage.
Returns
- object
The object represented by the settings storage.
IsPersistable(Type)
Determines whether the specified type is persistable.
public static bool IsPersistable(this Type type)
Parameters
typeTypeThe type to evaluate.
Returns
- bool
trueif the type implements IPersistable or IAsyncPersistable; otherwise,false.
IsSerializablePrimitive(Type)
Determines whether the specified type is a serializable primitive.
public static bool IsSerializablePrimitive(this Type type)
Parameters
typeTypeThe type to examine.
Returns
- bool
trueif the type is a primitive or Uri; otherwise,false.
Load(IPersistable, SettingsStorage, string)
Loads the state of the persistable object from the specified settings storage using the given key.
public static void Load(this IPersistable persistable, SettingsStorage settings, string name)
Parameters
persistableIPersistableThe persistable object to load.
settingsSettingsStorageThe settings storage.
namestringThe name of the value within the storage.
Load(SettingsStorage, Type)
Loads an IPersistable object of the specified type from the settings storage.
public static IPersistable Load(this SettingsStorage storage, Type type)
Parameters
storageSettingsStorageThe settings storage to load from.
typeTypeThe type of the persistable object.
Returns
- IPersistable
The loaded persistable object.
LoadAsync(SettingsStorage, Type, CancellationToken)
Asynchronously loads an asynchronous persistable object using the specified type.
public static ValueTask<IAsyncPersistable> LoadAsync(this SettingsStorage storage, Type type, CancellationToken cancellationToken = default)
Parameters
storageSettingsStorageThe settings storage to load from.
typeTypeThe type of the asynchronous persistable object.
cancellationTokenCancellationTokenA token for cancellation.
Returns
- ValueTask<IAsyncPersistable>
A ValueTask with the loaded asynchronous persistable object.
LoadAsync<T>(SettingsStorage, CancellationToken)
Asynchronously loads an asynchronous persistable object of type T.
public static ValueTask<T> LoadAsync<T>(this SettingsStorage storage, CancellationToken cancellationToken = default) where T : IAsyncPersistable, new()
Parameters
storageSettingsStorageThe settings storage to load from.
cancellationTokenCancellationTokenA token for cancellation.
Returns
- ValueTask<T>
A ValueTask with the loaded object of type T.
Type Parameters
TThe type of the asynchronous persistable object.
LoadEntire<T>(SettingsStorage)
Creates and initializes an object from the specified settings storage.
public static T LoadEntire<T>(this SettingsStorage storage) where T : IPersistable
Parameters
storageSettingsStorageThe settings storage used to create the object.
Returns
- T
The created and initialized object.
Type Parameters
TThe type of the persistable object.
LoadFromString(ISerializer<SettingsStorage>, IPersistable, string)
Loads the state of the persistable object from a string using the provided serializer.
public static void LoadFromString(this ISerializer<SettingsStorage> serializer, IPersistable persistable, string value)
Parameters
serializerISerializer<SettingsStorage>The serializer to use.
persistableIPersistableThe persistable object to load.
valuestringThe string representation of the state.
LoadFromString<TSerializer>(IPersistable, string)
[Obsolete("Use overload with serializer param.")]
public static void LoadFromString<TSerializer>(this IPersistable persistable, string value) where TSerializer : ISerializer<SettingsStorage>, new()
Parameters
persistableIPersistablevaluestring
Type Parameters
TSerializer
LoadFromString<TValue>(ISerializer<TValue>, string)
Loads a value of type TValue from a string using the provided serializer.
public static TValue LoadFromString<TValue>(this ISerializer<TValue> serializer, string value)
Parameters
serializerISerializer<TValue>The serializer to use.
valuestringThe string representation of the value.
Returns
- TValue
The deserialized value.
Type Parameters
TValueThe type of the value to load.
LoadFromString<TSerializer>(string)
[Obsolete("Use overload with serializer param.")]
public static SettingsStorage LoadFromString<TSerializer>(this string value) where TSerializer : ISerializer<SettingsStorage>, new()
Parameters
valuestring
Returns
Type Parameters
TSerializer
LoadIfNotNull(IPersistable, SettingsStorage)
Loads the state of the persistable object from the specified settings storage if the storage is not null.
public static bool LoadIfNotNull(this IPersistable persistable, SettingsStorage storage)
Parameters
persistableIPersistableThe persistable object to load.
storageSettingsStorageThe settings storage.
Returns
- bool
trueif the state was loaded; otherwise,false.
LoadIfNotNull(IPersistable, SettingsStorage, string)
Loads the state of the persistable object from the specified settings storage using the given key if it is not null.
public static bool LoadIfNotNull(this IPersistable persistable, SettingsStorage settings, string name)
Parameters
persistableIPersistableThe persistable object to load.
settingsSettingsStorageThe settings storage.
namestringThe name of the value within the storage.
Returns
- bool
trueif the state was loaded; otherwise,false.
Load<T>(SettingsStorage)
Loads an IPersistable object of type T from the settings storage.
public static T Load<T>(this SettingsStorage storage) where T : IPersistable
Parameters
storageSettingsStorageThe settings storage to load from.
Returns
- T
The loaded object of type T.
Type Parameters
TThe type of the persistable object.
RegisterAdapterType(Type, Type)
Registers the adapter type for the given type.
public static void RegisterAdapterType(this Type type, Type adapterType)
Parameters
typeTypeThe type for which the adapter is registered.
adapterTypeTypeThe adapter type to register.
RegisterCustomSerializer<T>(Func<T, SettingsStorage>, Func<SettingsStorage, T>)
Registers a custom serializer for a specific type.
public static void RegisterCustomSerializer<T>(Func<T, SettingsStorage> serialize, Func<SettingsStorage, T> deserialize)
Parameters
serializeFunc<T, SettingsStorage>A function to serialize the object to a settings storage.
deserializeFunc<SettingsStorage, T>A function to deserialize from a settings storage to the object.
Type Parameters
TThe type for which to register the serializer.
RemoveAdapterType(Type)
Removes the registered adapter type for the given type.
public static bool RemoveAdapterType(this Type type)
Parameters
typeTypeThe type whose adapter registration is removed.
Returns
- bool
trueif the adapter was removed; otherwise,false.
Save(IPersistable)
Saves the state of the persistable object to a settings storage.
public static SettingsStorage Save(this IPersistable persistable)
Parameters
persistableIPersistableThe persistable object to save.
Returns
- SettingsStorage
A settings storage containing the saved state.
SaveAsync(IAsyncPersistable, CancellationToken)
Asynchronously saves the state of the asynchronous persistable object to a settings storage.
public static ValueTask<SettingsStorage> SaveAsync(this IAsyncPersistable persistable, CancellationToken cancellationToken = default)
Parameters
persistableIAsyncPersistableThe asynchronous persistable object.
cancellationTokenCancellationTokenA token for cancellation.
Returns
- ValueTask<SettingsStorage>
A ValueTask with the settings storage containing the saved state.
SaveEntire(IPersistable, bool)
Saves the entire state of the persistable object into a new settings storage.
public static SettingsStorage SaveEntire(this IPersistable persistable, bool isAssemblyQualifiedName)
Parameters
persistableIPersistableThe persistable object to save.
isAssemblyQualifiedNameboolA value indicating whether the type name should be assembly qualified.
Returns
- SettingsStorage
A settings storage containing the saved state.
SaveToString(ISerializer<SettingsStorage>, IPersistable)
Saves the state of the persistable object to a string using the provided serializer.
public static string SaveToString(this ISerializer<SettingsStorage> serializer, IPersistable persistable)
Parameters
serializerISerializer<SettingsStorage>The serializer to use.
persistableIPersistableThe persistable object to save.
Returns
- string
A string representing the saved state.
SaveToString<TSerializer>(IPersistable)
[Obsolete("Use overload with serializer param.")]
public static string SaveToString<TSerializer>(this IPersistable persistable) where TSerializer : ISerializer<SettingsStorage>, new()
Parameters
persistableIPersistable
Returns
Type Parameters
TSerializer
SaveToString<TValue>(ISerializer<TValue>, TValue)
Saves the settings to a string using the provided serializer.
public static string SaveToString<TValue>(this ISerializer<TValue> serializer, TValue settings)
Parameters
serializerISerializer<TValue>The serializer to use.
settingsTValueThe settings to save.
Returns
- string
A string representing the settings.
Type Parameters
TValueThe type of the settings.
SaveToString<TSerializer>(SettingsStorage)
[Obsolete("Use overload with serializer param.")]
public static string SaveToString<TSerializer>(this SettingsStorage settings) where TSerializer : ISerializer<SettingsStorage>, new()
Parameters
settingsSettingsStorage
Returns
Type Parameters
TSerializer
SetValue(SettingsStorage, string, IPersistable)
Adds a persistable object's state as a value in the settings storage.
public static void SetValue(this SettingsStorage storage, string name, IPersistable persistable)
Parameters
storageSettingsStorageThe settings storage to update.
namestringThe name of the setting.
persistableIPersistableThe persistable object whose state is added.
ToMember(SettingsStorage)
Converts the settings storage to a MemberInfo.
public static MemberInfo ToMember(this SettingsStorage storage)
Parameters
storageSettingsStorageThe settings storage to convert.
Returns
- MemberInfo
The converted MemberInfo.
ToMember<T>(SettingsStorage)
Converts the settings storage to a member of type T.
public static T ToMember<T>(this SettingsStorage storage) where T : MemberInfo
Parameters
storageSettingsStorageThe settings storage to convert.
Returns
- T
The converted member of type T.
Type Parameters
TThe expected type of the member.
ToRefFive<T1, T2, T3, T4, T5>(SettingsStorage)
Converts the settings storage to a RefFive tuple of the specified types.
public static RefFive<T1, T2, T3, T4, T5> ToRefFive<T1, T2, T3, T4, T5>(this SettingsStorage storage)
Parameters
storageSettingsStorageThe settings storage to convert.
Returns
- RefFive<T1, T2, T3, T4, T5>
A RefFive tuple containing the converted values.
Type Parameters
T1The type of the first element.
T2The type of the second element.
T3The type of the third element.
T4The type of the fourth element.
T5The type of the fifth element.
ToRefPair<T1, T2>(SettingsStorage)
Converts the settings storage to a RefPair of the specified types.
public static RefPair<T1, T2> ToRefPair<T1, T2>(this SettingsStorage storage)
Parameters
storageSettingsStorageThe settings storage to convert.
Returns
- RefPair<T1, T2>
A RefPair containing the converted values.
Type Parameters
T1The type of the first element.
T2The type of the second element.
ToRefQuadruple<T1, T2, T3, T4>(SettingsStorage)
Converts the settings storage to a RefQuadruple of the specified types.
public static RefQuadruple<T1, T2, T3, T4> ToRefQuadruple<T1, T2, T3, T4>(this SettingsStorage storage)
Parameters
storageSettingsStorageThe settings storage to convert.
Returns
- RefQuadruple<T1, T2, T3, T4>
A RefQuadruple containing the converted values.
Type Parameters
T1The type of the first element.
T2The type of the second element.
T3The type of the third element.
T4The type of the fourth element.
ToRefTriple<T1, T2, T3>(SettingsStorage)
Converts the settings storage to a RefTriple of the specified types.
public static RefTriple<T1, T2, T3> ToRefTriple<T1, T2, T3>(this SettingsStorage storage)
Parameters
storageSettingsStorageThe settings storage to convert.
Returns
- RefTriple<T1, T2, T3>
A RefTriple containing the converted values.
Type Parameters
T1The type of the first element.
T2The type of the second element.
T3The type of the third element.
ToStorage(IRefTuple)
Converts a tuple implementing IRefTuple to a settings storage.
public static SettingsStorage ToStorage(this IRefTuple tuple)
Parameters
tupleIRefTupleThe tuple to convert.
Returns
- SettingsStorage
A settings storage representing the tuple.
ToStorage(object, bool)
Converts an object to a settings storage with type and value.
public static SettingsStorage ToStorage(this object value, bool isAssemblyQualifiedName = false)
Parameters
valueobjectThe object to convert.
isAssemblyQualifiedNameboolA value indicating whether the type name should be assembly qualified.
Returns
- SettingsStorage
A settings storage representing the object.
ToStorage<T>(T, bool)
Converts the specified member to a settings storage.
public static SettingsStorage ToStorage<T>(this T member, bool isAssemblyQualifiedName = false) where T : MemberInfo
Parameters
memberTThe member to convert.
isAssemblyQualifiedNameboolA value indicating whether the type name should be assembly qualified.
Returns
- SettingsStorage
A settings storage representing the member.
Type Parameters
TThe type of the member.
TryDeserialize<T>(SettingsStorage, out T)
Tries to deserialize a settings storage into an object of type T using a registered custom serializer.
public static bool TryDeserialize<T>(this SettingsStorage storage, out T value)
Parameters
storageSettingsStorageThe settings storage containing serialized data.
valueTWhen this method returns, contains the deserialized object if successful.
Returns
- bool
trueif deserialization was successful; otherwise,false.
Type Parameters
TThe type to deserialize.
TryGetAdapterType(Type, out Type)
Tries to get the registered adapter type for the given type.
public static bool TryGetAdapterType(this Type type, out Type adapterType)
Parameters
typeTypeThe type to check.
adapterTypeTypeWhen this method returns, contains the adapter type if found.
Returns
- bool
trueif found; otherwise,false.
TrySerialize<T>(T, out SettingsStorage)
Tries to serialize an object using a registered custom serializer.
public static bool TrySerialize<T>(this T value, out SettingsStorage storage)
Parameters
valueTThe object to serialize.
storageSettingsStorageWhen this method returns, contains the resulting settings storage if serialization was successful.
Returns
- bool
trueif serialization was successful; otherwise,false.
Type Parameters
TThe type of the object to serialize.
UnRegisterCustomSerializer<T>()
Unregisters the custom serializer for a specific type.
public static void UnRegisterCustomSerializer<T>()
Type Parameters
TThe type for which to unregister the serializer.