.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
ConverterDefaults.cs
Go to the documentation of this file.
1 // <copyright file=mitlicense.md url=http://lsauer.mit-license.org/ >
2 // Lo Sauer, 2013-2016
3 // </copyright>
4 // <summary> A tested, generic, portable, runtime-extensible type converter library </summary
5 // <language> C# > 6.0 </language>
6 // <version> 3.1.0.2 </version>
7 // <author> Lorenz Lo Sauer; people credited in the sources </author>
8 // <project> https://github.com/lsauer/dotnet-portable-type-cast </project>
9 namespace Core.TypeCast.Converters
10 {
11  using System;
12  using System.Globalization;
13  using Base;
14 
21  [System.Runtime.InteropServices.ComVisible(false)]
22  [Converter(loadOnDemand: true, nameSpace: nameof(System), dependencyInjection: true)]
24  {
29  private readonly Type self;
30 
34  public ConverterDefaults(IConverterCollection collection) : base(collection)
35  {
36  this.self = this.GetType();
37 
38  // use a custom number format, set in the ConverterCollection instance
39  this.NumberFormat = collection.Settings.NumberFormat as NumberFormatInfo;
40 
41  // convert from `string` to `System.T`
42  this.AddObjectConverter(collection: collection);
43  }
44 
49  public NumberFormatInfo NumberFormat { get; set; }
50 
56  private void AddObjectConverter(IConverterCollection collection)
57  {
58  collection.Add<object, int>(o => int.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
59  .Add<object, uint>(o => uint.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
60  .Add<object, char>(o => Convert.ToChar(o), this.self)
61  .Add<object, bool>(o => o != null && (int)o != 0 && o.ToString() != "false", this.self)
62  .Add<object, string>(o => o != null ? o.ToString() : string.Empty, this.self)
63  .Add<object, byte>(o => byte.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
64  .Add<object, sbyte>(o => sbyte.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
65  .Add<object, decimal>(o => decimal.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
66  .Add<object, double>(o => double.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
67  .Add<object, float>(o => float.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
68  .Add<object, long>(o => long.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
69  .Add<object, ulong>(o => ulong.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
70  .Add<object, short>(o => short.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
71  .Add<object, ushort>(o => ushort.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self)
72  .Add<object, Enum>(o => (Enum)Enum.Parse(o?.GetType(), o != null ? o.ToString() : string.Empty), this.self)
73  .Add<object, DateTime>(o => DateTime.Parse(o != null ? o.ToString() : string.Empty, this.NumberFormat), this.self);
74  }
75  }
76 }
The converter dependency provides loose implementation of Converters, merely constricting the constru...
IConverterCollection Add(Converter converter, Type baseType=null, bool allowDisambiguates=false, CancellationToken cancellationToken=default(CancellationToken))
Adds a Converter instance to the collection of ConverterCollection.Items
ConverterCollectionSettings Settings
The settings for the ConverterCollection.
ConverterDefaults(IConverterCollection collection)
Initializes a new instance of the ConverterDefaults class, and adds converters for the most common ty...
NumberFormatInfo NumberFormat
Gets or sets the number format used by default for Converter instances.
Converts between object and the most common System types in the System namespace. ...
The Converter base class, providing a simple container for conversion types, ConverterAttribute and c...
Definition: Converter.cs:22