Esempio n. 1
0
        private static void RegisterConverters(
            DefaultTypeConverter registry)
        {
            RegisterDateTimeConversions(registry);
            RegisterGuidConversions(registry);
            RegisterUriConversions(registry);
            RegisterBooleanConversions(registry);
            RegisterStringConversions(registry);
            RegisterNameStringConversions(registry);

            RegisterByteConversions(registry);

            RegisterUInt16Conversions(registry);
            RegisterUInt32Conversions(registry);
            RegisterUInt64Conversions(registry);

            RegisterInt16Conversions(registry);
            RegisterInt32Conversions(registry);
            RegisterInt64Conversions(registry);

            RegisterSingleConversions(registry);
            RegisterDoubleConversions(registry);
            RegisterDecimalConversions(registry);

            RegisterStringListConversions(registry);
        }
Esempio n. 2
0
 private static void RegisterBooleanConversions(
     DefaultTypeConverter registry)
 {
     registry.Register <bool, string>(from =>
                                      from.ToString(CultureInfo.InvariantCulture));
     registry.Register <bool, short>(from =>
                                     from ? (short)1 : (short)0);
     registry.Register <bool, int>(from =>
                                   from ? (int)1 : (int)0);
     registry.Register <bool, long>(from =>
                                    from ? (long)1 : (long)0);
 }
Esempio n. 3
0
 private static void RegisterDecimalConversions(
     DefaultTypeConverter registry)
 {
     registry.Register <decimal, short>(from => SysConv.ToInt16(from));
     registry.Register <decimal, int>(from => SysConv.ToInt32(from));
     registry.Register <decimal, long>(from => SysConv.ToInt64(from));
     registry.Register <decimal, ushort>(from => SysConv.ToUInt16(from));
     registry.Register <decimal, uint>(from => SysConv.ToUInt32(from));
     registry.Register <decimal, ulong>(from => SysConv.ToUInt64(from));
     registry.Register <decimal, float>(from => SysConv.ToSingle(from));
     registry.Register <decimal, double>(from => SysConv.ToDouble(from));
     registry.Register <decimal, string>(from =>
                                         from.ToString("E", CultureInfo.InvariantCulture));
 }
Esempio n. 4
0
        private static void RegisterDateTimeConversions(
            DefaultTypeConverter registry)
        {
            registry.Register <DateTimeOffset, DateTime>(
                from => from.UtcDateTime);
            registry.Register <DateTime, DateTimeOffset>(
                from => (DateTimeOffset)from);

            registry.Register <DateTimeOffset, long>(
                from => from.ToUnixTimeSeconds());
            registry.Register <long, DateTimeOffset>(
                from => DateTimeOffset.FromUnixTimeSeconds(from));

            registry.Register <DateTime, long>(
                from => ((DateTimeOffset)from).ToUnixTimeSeconds());
            registry.Register <long, DateTime>(
                from => DateTimeOffset.FromUnixTimeSeconds(from).UtcDateTime);

            registry.Register <DateTimeOffset, string>(
                from =>
            {
                if (from.Offset == TimeSpan.Zero)
                {
                    return(from.ToString(
                               _utcFormat,
                               CultureInfo.InvariantCulture));
                }

                return(from.ToString(
                           _localFormat,
                           CultureInfo.InvariantCulture));
            });
            registry.Register <DateTime, string>(
                from =>
            {
                var offset = new DateTimeOffset(from);

                if (offset.Offset == TimeSpan.Zero)
                {
                    return(offset.ToString(
                               _utcFormat,
                               CultureInfo.InvariantCulture));
                }

                return(offset.ToString(
                           _localFormat,
                           CultureInfo.InvariantCulture));
            });
        }
Esempio n. 5
0
 private static void RegisterDoubleConversions(
     DefaultTypeConverter registry)
 {
     registry.Register <double, byte>(from => SysConv.ToByte(from));
     registry.Register <double, short>(from => SysConv.ToInt16(from));
     registry.Register <double, int>(from => SysConv.ToInt32(from));
     registry.Register <double, long>(from => SysConv.ToInt64(from));
     registry.Register <double, ushort>(from => SysConv.ToUInt16(from));
     registry.Register <double, uint>(from => SysConv.ToUInt32(from));
     registry.Register <double, ulong>(from => SysConv.ToUInt64(from));
     registry.Register <double, decimal>(from => SysConv.ToDecimal(from));
     registry.Register <double, float>(from => SysConv.ToSingle(from));
     registry.Register <double, string>(from =>
                                        from.ToString(CultureInfo.InvariantCulture));
 }
Esempio n. 6
0
 private static void RegisterStringConversions(
     DefaultTypeConverter registry)
 {
     registry.Register <string, Guid>(from => Guid.Parse(from));
     registry.Register <string, Uri>(from =>
                                     new Uri(from, UriKind.RelativeOrAbsolute));
     registry.Register <string, short>(from =>
                                       short.Parse(from, NumberStyles.Integer,
                                                   CultureInfo.InvariantCulture));
     registry.Register <string, int>(from =>
                                     int.Parse(from, NumberStyles.Integer,
                                               CultureInfo.InvariantCulture));
     registry.Register <string, long>(from =>
                                      long.Parse(from, NumberStyles.Integer,
                                                 CultureInfo.InvariantCulture));
     registry.Register <string, ushort>(from =>
                                        ushort.Parse(from, NumberStyles.Integer,
                                                     CultureInfo.InvariantCulture));
     registry.Register <string, uint>(from =>
                                      uint.Parse(from, NumberStyles.Integer,
                                                 CultureInfo.InvariantCulture));
     registry.Register <string, ulong>(from =>
                                       ulong.Parse(from, NumberStyles.Integer,
                                                   CultureInfo.InvariantCulture));
     registry.Register <string, float>(from =>
                                       ushort.Parse(from, NumberStyles.Float,
                                                    CultureInfo.InvariantCulture));
     registry.Register <string, double>(from =>
                                        double.Parse(from, NumberStyles.Float,
                                                     CultureInfo.InvariantCulture));
     registry.Register <string, decimal>(from =>
                                         decimal.Parse(from, NumberStyles.Float,
                                                       CultureInfo.InvariantCulture));
     registry.Register <string, bool>(from =>
                                      bool.Parse(from));
     registry.Register <string, NameString>(from => from);
 }
Esempio n. 7
0
        private static void RegisterStringListConversions(
            DefaultTypeConverter registry)
        {
            registry.Register <IEnumerable <string>, string>(
                from => string.Join(",", from));

            registry.Register <IReadOnlyCollection <string>, string>(
                from => string.Join(",", from));

            registry.Register <IReadOnlyList <string>, string>(
                from => string.Join(",", from));

            registry.Register <ICollection <string>, string>(
                from => string.Join(",", from));

            registry.Register <IList <string>, string>(
                from => string.Join(",", from));

            registry.Register <string[], string>(
                from => string.Join(",", from));

            registry.Register <List <string>, string>(
                from => string.Join(",", from));
        }
Esempio n. 8
0
 private static void RegisterUriConversions(
     DefaultTypeConverter registry)
 {
     registry.Register <Uri, string>(from => from.ToString());
 }
Esempio n. 9
0
 private static void RegisterGuidConversions(
     DefaultTypeConverter registry)
 {
     registry.Register <Guid, string>(from => from.ToString("N"));
 }
Esempio n. 10
0
 private static void RegisterNameStringConversions(
     DefaultTypeConverter registry)
 {
     registry.Register <NameString, string>(from => from);
 }