// Copyright (c) All contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; using MessagePack.Formatters; using MessagePack.Internal; #pragma warning disable SA1403 // File may only contain a single namespace namespace MessagePack.Resolvers { public sealed class NativeDateTimeResolver : IFormatterResolver { /// /// The singleton instance that can be used. /// public static readonly NativeDateTimeResolver Instance; /// /// A instance with this formatter pre-configured. /// public static readonly MessagePackSerializerOptions Options; static NativeDateTimeResolver() { Instance = new NativeDateTimeResolver(); Options = new MessagePackSerializerOptions(Instance); } private NativeDateTimeResolver() { } public IMessagePackFormatter GetFormatter() { return FormatterCache.Formatter; } private static class FormatterCache { public static readonly IMessagePackFormatter Formatter; static FormatterCache() { Formatter = (IMessagePackFormatter)NativeDateTimeResolverGetFormatterHelper.GetFormatter(typeof(T)); } } } } namespace MessagePack.Internal { internal static class NativeDateTimeResolverGetFormatterHelper { internal static object GetFormatter(Type t) { if (t == typeof(DateTime)) { return NativeDateTimeFormatter.Instance; } else if (t == typeof(DateTime?)) { return new StaticNullableFormatter(NativeDateTimeFormatter.Instance); } else if (t == typeof(DateTime[])) { return NativeDateTimeArrayFormatter.Instance; } return null; } } }