mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-24 14:34:06 +02:00
Add SingleTypeConverter and update names in FileSerializer
This commit is contained in:
parent
38c2781cd3
commit
d431b63c27
@ -13,15 +13,15 @@ class FileSerializer<T> where T : ISerializedObject{
|
|||||||
private static readonly ITypeConverter BasicSerializerObj = new BasicSerializer();
|
private static readonly ITypeConverter BasicSerializerObj = new BasicSerializer();
|
||||||
|
|
||||||
private readonly Dictionary<string, PropertyInfo> props;
|
private readonly Dictionary<string, PropertyInfo> props;
|
||||||
private readonly Dictionary<Type, ITypeConverter> serializers;
|
private readonly Dictionary<Type, ITypeConverter> converters;
|
||||||
|
|
||||||
public FileSerializer(){
|
public FileSerializer(){
|
||||||
this.props = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.CanWrite).ToDictionary(prop => prop.Name);
|
this.props = typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public).Where(prop => prop.CanWrite).ToDictionary(prop => prop.Name);
|
||||||
this.serializers = new Dictionary<Type, ITypeConverter>();
|
this.converters = new Dictionary<Type, ITypeConverter>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterSerializer(Type type, ITypeConverter serializer){
|
public void RegisterTypeConverter(Type type, ITypeConverter converter){
|
||||||
serializers[type] = serializer;
|
converters[type] = converter;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Write(string file, T obj){
|
public void Write(string file, T obj){
|
||||||
@ -30,7 +30,7 @@ public void Write(string file, T obj){
|
|||||||
Type type = prop.Value.PropertyType;
|
Type type = prop.Value.PropertyType;
|
||||||
object value = prop.Value.GetValue(obj);
|
object value = prop.Value.GetValue(obj);
|
||||||
|
|
||||||
if (!serializers.TryGetValue(type, out ITypeConverter serializer)) {
|
if (!converters.TryGetValue(type, out ITypeConverter serializer)) {
|
||||||
serializer = BasicSerializerObj;
|
serializer = BasicSerializerObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ public void Read(string file, T obj){
|
|||||||
string value = line.Substring(space+1).Replace(NewLineCustom, Environment.NewLine);
|
string value = line.Substring(space+1).Replace(NewLineCustom, Environment.NewLine);
|
||||||
|
|
||||||
if (props.TryGetValue(property, out PropertyInfo info)){
|
if (props.TryGetValue(property, out PropertyInfo info)){
|
||||||
if (!serializers.TryGetValue(info.PropertyType, out ITypeConverter serializer)) {
|
if (!converters.TryGetValue(info.PropertyType, out ITypeConverter serializer)) {
|
||||||
serializer = BasicSerializerObj;
|
serializer = BasicSerializerObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
31
Data/Serialization/SingleTypeConverter.cs
Normal file
31
Data/Serialization/SingleTypeConverter.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace TweetDuck.Data.Serialization{
|
||||||
|
class SingleTypeConverter<T> : ITypeConverter{
|
||||||
|
public delegate string FuncConvertToString<U>(U value);
|
||||||
|
public delegate U FuncConvertToObject<U>(string value);
|
||||||
|
|
||||||
|
public FuncConvertToString<T> ConvertToString { get; set; }
|
||||||
|
public FuncConvertToObject<T> ConvertToObject { get; set; }
|
||||||
|
|
||||||
|
bool ITypeConverter.TryWriteType(Type type, object value, out string converted){
|
||||||
|
try{
|
||||||
|
converted = ConvertToString((T)value);
|
||||||
|
return true;
|
||||||
|
}catch{
|
||||||
|
converted = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ITypeConverter.TryReadType(Type type, string value, out object converted){
|
||||||
|
try{
|
||||||
|
converted = ConvertToObject(value);
|
||||||
|
return true;
|
||||||
|
}catch{
|
||||||
|
converted = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -205,6 +205,7 @@
|
|||||||
<Compile Include="Data\InjectedHTML.cs" />
|
<Compile Include="Data\InjectedHTML.cs" />
|
||||||
<Compile Include="Data\Serialization\ISerializedObject.cs" />
|
<Compile Include="Data\Serialization\ISerializedObject.cs" />
|
||||||
<Compile Include="Data\Serialization\ITypeConverter.cs" />
|
<Compile Include="Data\Serialization\ITypeConverter.cs" />
|
||||||
|
<Compile Include="Data\Serialization\SingleTypeConverter.cs" />
|
||||||
<Compile Include="Data\TwoKeyDictionary.cs" />
|
<Compile Include="Data\TwoKeyDictionary.cs" />
|
||||||
<Compile Include="Data\WindowState.cs" />
|
<Compile Include="Data\WindowState.cs" />
|
||||||
<Compile Include="Core\Utils\WindowsUtils.cs" />
|
<Compile Include="Core\Utils\WindowsUtils.cs" />
|
||||||
|
Loading…
Reference in New Issue
Block a user