This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from lukasz-pyrzyk/preparationForStream
Preparation for stream
- Loading branch information
Showing
41 changed files
with
357 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using BinaryFormatter.TypeConverter; | ||
using BinaryFormatter.Types; | ||
|
||
namespace BinaryFormatter | ||
{ | ||
internal class ConvertersSelector | ||
{ | ||
private static readonly Dictionary<Type, BaseTypeConverter> _converters = new Dictionary<Type, BaseTypeConverter> | ||
{ | ||
[typeof(byte)] = new ByteConverter(), | ||
[typeof(sbyte)] = new SByteConverter(), | ||
[typeof(char)] = new CharConverter(), | ||
[typeof(short)] = new ShortConverter(), | ||
[typeof(ushort)] = new UShortConverter(), | ||
[typeof(int)] = new IntConverter(), | ||
[typeof(uint)] = new UIntConverter(), | ||
[typeof(long)] = new LongConverter(), | ||
[typeof(ulong)] = new ULongConverter(), | ||
[typeof(float)] = new FloatConverter(), | ||
[typeof(double)] = new DoubleConverter(), | ||
[typeof(bool)] = new BoolConverter(), | ||
[typeof(decimal)] = new DecimalConverter(), | ||
[typeof(string)] = new StringConverter(), | ||
[typeof(DateTime)] = new DatetimeConverter(), | ||
[typeof(byte[])] = new ByteArrayConverter(), | ||
[typeof(IEnumerable)] = new IEnumerableConverter() | ||
}; | ||
|
||
public BaseTypeConverter SelectConverter(object obj) | ||
{ | ||
if(obj == null) return null; | ||
Type type = obj.GetType(); | ||
return SelectConverter(type); | ||
} | ||
|
||
public BaseTypeConverter SelectConverter(Type type) | ||
{ | ||
BaseTypeConverter converter; | ||
if (_converters.TryGetValue(type, out converter)) | ||
{ | ||
return converter; | ||
} | ||
|
||
bool isEnumerableType = type.GetTypeInfo().ImplementedInterfaces.Any(t => t == typeof(IEnumerable)); | ||
if (isEnumerableType) | ||
{ | ||
if (_converters.TryGetValue(typeof(IEnumerable), out converter)) | ||
{ | ||
return converter; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public BaseTypeConverter ForSerializedType(SerializedType type) | ||
{ | ||
return _converters.First(x => x.Value.Type == type).Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.