1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-06 23:34:05 +02:00

Add SingleTypeConverter and update names in FileSerializer

This commit is contained in:
chylex 2017-07-07 01:47:14 +02:00
parent 38c2781cd3
commit d431b63c27
3 changed files with 38 additions and 6 deletions

View File

@ -13,15 +13,15 @@ class FileSerializer<T> where T : ISerializedObject{
private static readonly ITypeConverter BasicSerializerObj = new BasicSerializer();
private readonly Dictionary<string, PropertyInfo> props;
private readonly Dictionary<Type, ITypeConverter> serializers;
private readonly Dictionary<Type, ITypeConverter> converters;
public FileSerializer(){
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){
serializers[type] = serializer;
public void RegisterTypeConverter(Type type, ITypeConverter converter){
converters[type] = converter;
}
public void Write(string file, T obj){
@ -30,7 +30,7 @@ public void Write(string file, T obj){
Type type = prop.Value.PropertyType;
object value = prop.Value.GetValue(obj);
if (!serializers.TryGetValue(type, out ITypeConverter serializer)) {
if (!converters.TryGetValue(type, out ITypeConverter serializer)) {
serializer = BasicSerializerObj;
}
@ -60,7 +60,7 @@ public void Read(string file, T obj){
string value = line.Substring(space+1).Replace(NewLineCustom, Environment.NewLine);
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;
}

View 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;
}
}
}
}

View File

@ -205,6 +205,7 @@
<Compile Include="Data\InjectedHTML.cs" />
<Compile Include="Data\Serialization\ISerializedObject.cs" />
<Compile Include="Data\Serialization\ITypeConverter.cs" />
<Compile Include="Data\Serialization\SingleTypeConverter.cs" />
<Compile Include="Data\TwoKeyDictionary.cs" />
<Compile Include="Data\WindowState.cs" />
<Compile Include="Core\Utils\WindowsUtils.cs" />