PersistableHelper
Provides helper methods for persisting, cloning, loading and saving objects.
方法
public static ValueTask ApplyAsync<T>(T obj, T clone, CancellationToken cancellationToken)
result = PersistableHelper.ApplyAsync(obj, clone, cancellationToken)
Asynchronously applies the state from the clone to the target asynchronous persistable object.
- obj
- The target object.
- clone
- The object from which to copy the state.
- cancellationToken
- A token for cancellation.
返回值: A ValueTask representing the asynchronous operation.
public static void Apply<T>(T obj, T clone)
PersistableHelper.Apply(obj, clone)
Applies the state from the clone to the target persistable object.
- obj
- The target object.
- clone
- The object from which to copy the state.
public static ValueTask<T> CloneAsync<T>(T obj, CancellationToken cancellationToken)
result = PersistableHelper.CloneAsync(obj, cancellationToken)
Asynchronously clones the specified asynchronous persistable object.
- obj
- The object to clone.
- cancellationToken
- A token for cancellation.
返回值: A ValueTask with the cloned object.
public static T Clone<T>(T obj)
result = PersistableHelper.Clone(obj)
Clones the specified persistable object.
- obj
- The object to clone.
返回值: A clone of the object.
public static void ForceLoad<T>(T t, SettingsStorage storage)
PersistableHelper.ForceLoad(t, storage)
Forces the persistable object to load its state from the given settings storage.
- t
- The target object.
- storage
- The settings storage to load from.
public static object FromStorage(SettingsStorage storage)
result = PersistableHelper.FromStorage(storage)
Converts the settings storage back to an object.
- storage
- The settings storage.
返回值: The object represented by the settings storage.
public static bool IsPersistable(Type type)
result = PersistableHelper.IsPersistable(type)
Determines whether the specified type is persistable.
- type
- The type to evaluate.
返回值: true if the type implements IPersistable or IAsyncPersistable; otherwise, false.
public static bool IsSerializablePrimitive(Type type)
result = PersistableHelper.IsSerializablePrimitive(type)
Determines whether the specified type is a serializable primitive.
- type
- The type to examine.
返回值: true if the type is a primitive or Uri; otherwise, false.
public static IPersistable Load(SettingsStorage storage, Type type)
result = PersistableHelper.Load(storage, type)
Loads an IPersistable object of the specified type from the settings storage.
- storage
- The settings storage to load from.
- type
- The type of the persistable object.
返回值: The loaded persistable object.
public static void Load(IPersistable persistable, SettingsStorage settings, string name)
PersistableHelper.Load(persistable, settings, name)
Loads the state of the persistable object from the specified settings storage using the given key.
- persistable
- The persistable object to load.
- settings
- The settings storage.
- name
- The name of the value within the storage.
public static ValueTask<IAsyncPersistable> LoadAsync(SettingsStorage storage, Type type, CancellationToken cancellationToken)
result = PersistableHelper.LoadAsync(storage, type, cancellationToken)
Asynchronously loads an asynchronous persistable object using the specified type.
- storage
- The settings storage to load from.
- type
- The type of the asynchronous persistable object.
- cancellationToken
- A token for cancellation.
返回值: A ValueTask with the loaded asynchronous persistable object.
public static ValueTask<T> LoadAsync<T>(SettingsStorage storage, CancellationToken cancellationToken)
result = PersistableHelper.LoadAsync(storage, cancellationToken)
Asynchronously loads an asynchronous persistable object of type T.
- storage
- The settings storage to load from.
- cancellationToken
- A token for cancellation.
返回值: A ValueTask with the loaded object of type T.
public static T LoadEntire<T>(SettingsStorage storage)
result = PersistableHelper.LoadEntire(storage)
Creates and initializes an object from the specified settings storage.
- storage
- The settings storage used to create the object.
返回值: The created and initialized object.
public static void LoadFromString(ISerializer<SettingsStorage> serializer, IPersistable persistable, string value)
PersistableHelper.LoadFromString(serializer, persistable, value)
Loads the state of the persistable object from a string using the provided serializer.
- serializer
- The serializer to use.
- persistable
- The persistable object to load.
- value
- The string representation of the state.
public static TValue LoadFromString<TValue>(ISerializer<TValue> serializer, string value)
result = PersistableHelper.LoadFromString(serializer, value)
Loads a value of type TValue from a string using the provided serializer.
- serializer
- The serializer to use.
- value
- The string representation of the value.
返回值: The deserialized value.
public static bool LoadIfNotNull(IPersistable persistable, SettingsStorage storage)
result = PersistableHelper.LoadIfNotNull(persistable, storage)
Loads the state of the persistable object from the specified settings storage if the storage is not null.
- persistable
- The persistable object to load.
- storage
- The settings storage.
返回值: true if the state was loaded; otherwise, false.
public static bool LoadIfNotNull(IPersistable persistable, SettingsStorage settings, string name)
result = PersistableHelper.LoadIfNotNull(persistable, settings, name)
Loads the state of the persistable object from the specified settings storage using the given key if it is not null.
- persistable
- The persistable object to load.
- settings
- The settings storage.
- name
- The name of the value within the storage.
返回值: true if the state was loaded; otherwise, false.
public static T Load<T>(SettingsStorage storage)
result = PersistableHelper.Load(storage)
Loads an IPersistable object of type T from the settings storage.
- storage
- The settings storage to load from.
返回值: The loaded object of type T.
public static void RegisterAdapterType(Type type, Type adapterType)
PersistableHelper.RegisterAdapterType(type, adapterType)
Registers the adapter type for the given type.
- type
- The type for which the adapter is registered.
- adapterType
- The adapter type to register.
public static void RegisterCustomSerializer<T>(Func<T, SettingsStorage> serialize, Func<SettingsStorage, T> deserialize)
PersistableHelper.RegisterCustomSerializer(serialize, deserialize)
Registers a custom serializer for a specific type.
- serialize
- A function to serialize the object to a settings storage.
- deserialize
- A function to deserialize from a settings storage to the object.
public static bool RemoveAdapterType(Type type)
result = PersistableHelper.RemoveAdapterType(type)
Removes the registered adapter type for the given type.
- type
- The type whose adapter registration is removed.
返回值: true if the adapter was removed; otherwise, false.
public static SettingsStorage Save(IPersistable persistable)
result = PersistableHelper.Save(persistable)
Saves the state of the persistable object to a settings storage.
- persistable
- The persistable object to save.
返回值: A settings storage containing the saved state.
public static ValueTask<SettingsStorage> SaveAsync(IAsyncPersistable persistable, CancellationToken cancellationToken)
result = PersistableHelper.SaveAsync(persistable, cancellationToken)
Asynchronously saves the state of the asynchronous persistable object to a settings storage.
- persistable
- The asynchronous persistable object.
- cancellationToken
- A token for cancellation.
返回值: A ValueTask with the settings storage containing the saved state.
public static SettingsStorage SaveEntire(IPersistable persistable, bool isAssemblyQualifiedName)
result = PersistableHelper.SaveEntire(persistable, isAssemblyQualifiedName)
Saves the entire state of the persistable object into a new settings storage.
- persistable
- The persistable object to save.
- isAssemblyQualifiedName
- A value indicating whether the type name should be assembly qualified.
返回值: A settings storage containing the saved state.
public static string SaveToString(ISerializer<SettingsStorage> serializer, IPersistable persistable)
result = PersistableHelper.SaveToString(serializer, persistable)
Saves the state of the persistable object to a string using the provided serializer.
- serializer
- The serializer to use.
- persistable
- The persistable object to save.
返回值: A string representing the saved state.
public static string SaveToString<TValue>(ISerializer<TValue> serializer, TValue settings)
result = PersistableHelper.SaveToString(serializer, settings)
Saves the settings to a string using the provided serializer.
- serializer
- The serializer to use.
- settings
- The settings to save.
返回值: A string representing the settings.
public static void SetValue(SettingsStorage storage, string name, IPersistable persistable)
PersistableHelper.SetValue(storage, name, persistable)
Adds a persistable object's state as a value in the settings storage.
- storage
- The settings storage to update.
- name
- The name of the setting.
- persistable
- The persistable object whose state is added.
public static MemberInfo ToMember(SettingsStorage storage)
result = PersistableHelper.ToMember(storage)
Converts the settings storage to a MemberInfo.
- storage
- The settings storage to convert.
返回值: The converted MemberInfo.
public static T ToMember<T>(SettingsStorage storage)
result = PersistableHelper.ToMember(storage)
Converts the settings storage to a member of type T.
- storage
- The settings storage to convert.
返回值: The converted member of type T.
public static RefFive<T1, T2, T3, T4, T5> ToRefFive<T1, T2, T3, T4, T5>(SettingsStorage storage)
result = PersistableHelper.ToRefFive(storage)
Converts the settings storage to a RefFive tuple of the specified types.
- storage
- The settings storage to convert.
返回值: A RefFive tuple containing the converted values.
public static RefPair<T1, T2> ToRefPair<T1, T2>(SettingsStorage storage)
result = PersistableHelper.ToRefPair(storage)
Converts the settings storage to a RefPair of the specified types.
- storage
- The settings storage to convert.
返回值: A RefPair containing the converted values.
public static RefQuadruple<T1, T2, T3, T4> ToRefQuadruple<T1, T2, T3, T4>(SettingsStorage storage)
result = PersistableHelper.ToRefQuadruple(storage)
Converts the settings storage to a RefQuadruple of the specified types.
- storage
- The settings storage to convert.
返回值: A RefQuadruple containing the converted values.
public static RefTriple<T1, T2, T3> ToRefTriple<T1, T2, T3>(SettingsStorage storage)
result = PersistableHelper.ToRefTriple(storage)
Converts the settings storage to a RefTriple of the specified types.
- storage
- The settings storage to convert.
返回值: A RefTriple containing the converted values.
public static SettingsStorage ToStorage(IRefTuple tuple)
result = PersistableHelper.ToStorage(tuple)
Converts a tuple implementing IRefTuple to a settings storage.
- tuple
- The tuple to convert.
返回值: A settings storage representing the tuple.
public static SettingsStorage ToStorage(object value, bool isAssemblyQualifiedName)
result = PersistableHelper.ToStorage(value, isAssemblyQualifiedName)
Converts an object to a settings storage with type and value.
- value
- The object to convert.
- isAssemblyQualifiedName
- A value indicating whether the type name should be assembly qualified.
返回值: A settings storage representing the object.
public static SettingsStorage ToStorage<T>(T member, bool isAssemblyQualifiedName)
result = PersistableHelper.ToStorage(member, isAssemblyQualifiedName)
Converts the specified member to a settings storage.
- member
- The member to convert.
- isAssemblyQualifiedName
- A value indicating whether the type name should be assembly qualified.
返回值: A settings storage representing the member.
public static bool TryDeserialize<T>(SettingsStorage storage, T value)
result = PersistableHelper.TryDeserialize(storage, value)
Tries to deserialize a settings storage into an object of type T using a registered custom serializer.
- storage
- The settings storage containing serialized data.
- value
- When this method returns, contains the deserialized object if successful.
返回值: true if deserialization was successful; otherwise, false.
public static bool TryGetAdapterType(Type type, Type adapterType)
result = PersistableHelper.TryGetAdapterType(type, adapterType)
Tries to get the registered adapter type for the given type.
- type
- The type to check.
- adapterType
- When this method returns, contains the adapter type if found.
返回值: true if found; otherwise, false.
public static bool TrySerialize<T>(T value, SettingsStorage storage)
result = PersistableHelper.TrySerialize(value, storage)
Tries to serialize an object using a registered custom serializer.
- value
- The object to serialize.
- storage
- When this method returns, contains the resulting settings storage if serialization was successful.
返回值: true if serialization was successful; otherwise, false.
public static void UnRegisterCustomSerializer<T>()
PersistableHelper.UnRegisterCustomSerializer()
Unregisters the custom serializer for a specific type.