Resolvers
BuildInResolver.cs
//using System.Runtime.CompilerServices;
using Bssom.Serializer.Formatters;
using Bssom.Serializer.Internal;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
namespace Bssom.Serializer.Resolvers
{
///
/// Provides formatters for ,,,,,,,, , Provides nullable inline formatters for types in the
///
public sealed clast BuildInResolver : IFormatterResolver
{
///
/// The singleton instance that can be used.
///
public static readonly BuildInResolver Instance = new BuildInResolver();
public IBssomFormatter GetFormatter()
{
return FormatterCache.Formatter;
}
private static clast FormatterCache
{
public static readonly IBssomFormatter Formatter;
static FormatterCache()
{
Type t = typeof(T);
if (BuildInResolverGetFormatterHelper.TryGetFormatter(t, out IBssomFormatter formatter))
{
Formatter = (IBssomFormatter)formatter;
return;
}
if (t.IsEnum)
{
Formatter = EnumFormatter.Instance;
return;
}
if (t.IsGenericType)
{
Type genericType = t.GetGenericTypeDefinition();
if (genericType == typeof(Nullable))
{
Formatter = (IBssomFormatter)Activator.CreateInstance(typeof(NullableFormatter).MakeGenericType(t.GetGenericArguments()));
return;
}
else if (genericType == typeof(Lazy))
{
Formatter = (IBssomFormatter)Activator.CreateInstance(typeof(LazyFormatter).MakeGenericType(t.GetGenericArguments()));
return;
}
else if (genericType == typeof(IGrouping))
{
Formatter = (IBssomFormatter)Activator.CreateInstance(typeof(InterfaceGroupingFormatter).MakeGenericType(t.GetGenericArguments()));
return;
}
else if (genericType == typeof(ILookup))
{
Formatter = (IBssomFormatter)Activator.CreateInstance(typeof(InterfaceILookupFormatter).MakeGenericType(t.GetGenericArguments()));
return;
}
}
if (t.IsAnonymousType())
{
Formatter = AnonymousTypeFormatter.Instance;
return;
}
}
}
}
}
namespace Bssom.Serializer.Internal
{
internal static clast BuildInResolverGetFormatterHelper
{
private static readonly Dictionary FormatterMap = new Dictionary()
{
{ typeof(StringDictionary), StringDictionaryFormatter.Instance },
{ typeof(StringBuilder), StringBuilderFormatter.Instance },
{ typeof(BitArray), BitArrayFormatter.Instance },
{ typeof(NameValueCollection),NameValueCollectionFormatter.Instance },
{ typeof(Version),VersionFormatter.Instance },
{ typeof(Uri),UriFormatter.Instance },
{ typeof(TimeSpan), TimeSpanFormatter.Instance },
{ typeof(DBNull), DBNullFormatter.Instance },
{ typeof(DataTable), DataTableFormatter.Instance },
{ typeof(sbyte?), StaticNullableInt8Formatter.Instance },
{ typeof(Int16?), StaticNullableInt16Formatter.Instance },
{ typeof(Int32?), StaticNullableInt32Formatter.Instance },
{ typeof(Int64?), StaticNullableInt64Formatter.Instance },
{ typeof(byte?), StaticNullableUInt8Formatter.Instance },
{ typeof(UInt16?), StaticNullableUInt16Formatter.Instance },
{ typeof(UInt32?), StaticNullableUInt32Formatter.Instance },
{ typeof(UInt64?), StaticNullableUInt64Formatter.Instance },
{ typeof(Single?), StaticNullableFloat32Formatter.Instance },
{ typeof(Double?), StaticNullableFloat64Formatter.Instance },
{ typeof(bool?), StaticNullableBooleanFormatter.Instance },
{ typeof(char?), StaticNullableCharFormatter.Instance },
{ typeof(Guid?), StaticNullableGuidFormatter.Instance },
{ typeof(Decimal?), StaticNullableDecimalFormatter.Instance },
};
internal static bool TryGetFormatter(Type t, out IBssomFormatter formatter)
{
if (FormatterMap.TryGetValue(t, out formatter))
{
return true;
}
return false;
}
}
}