.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
ConverterCollection.AddBuilder.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
10 {
11  using System;
12  using System.Collections;
13  using System.Collections.Concurrent;
14  using System.Collections.Generic;
15  using System.Globalization;
16  using System.Linq;
17  using System.Linq.Expressions;
18  using System.Reflection;
19  using System.Runtime.CompilerServices;
20  using System.Runtime.InteropServices;
21  using System.Threading;
22 
23  using Core.Extensions;
24  using Core.Singleton;
25  using Core.TypeCast.Base;
26 
27 
37  public partial class ConverterCollection : Singleton<ConverterCollection>, IConverterCollection, IDisposable, IEnumerable<Converter>, IQueryable<Converter>
38  {
45  public class AddBuilder<TBase> where TBase : class
46  {
50  ConverterCollection baseClass;
51 
55  CancellationToken cancellationToken;
56 
60  List<Action> actions;
61 
66 
75  public AddBuilder(ConverterCollection converterCollection,
76  ConverterCollectionSettings settings = null,
77  CancellationToken cancellationToken = default(CancellationToken))
78  : this(converterCollection: converterCollection, settings: settings, cancellationToken: ref cancellationToken)
79  {
80 
81  }
82 
91  public AddBuilder(ConverterCollection converterCollection,
93  ref CancellationToken cancellationToken)
94  {
95  this.baseClass = converterCollection;
96  this.settings = settings;
97  this.cancellationToken = cancellationToken;
98  this.actions = new List<Action>();
99  }
100 
111  public AddBuilder<TBase> Add<TIn, TOut>(Func<TIn, TOut> converterAction)
112  {
113  actions.Add(() => this.baseClass.Add<TIn, TOut, TBase>(converterAction, this.cancellationToken));
114  return this;
115  }
116 
127  public AddBuilder<TBase> Add<TIn, TOut>(Func<TIn, TOut, TOut> converterActionDefault)
128  {
129  actions.Add(() => this.baseClass.Add<TIn, TOut, TBase>(converterActionDefault, this.cancellationToken));
130  return this;
131  }
132 
138  {
139  return baseClass;
140  }
141 
147  {
148  // temporarily switch out settings for any passed builder-settings argument
149  if(this.settings != null)
150  {
151  var tmp = this.baseClass.Settings;
152  this.baseClass.Settings = this.settings;
153  actions.Add(() => this.baseClass.Settings = tmp);
154  }
155  actions.Count(a => { a.Invoke(); return true; });
156  return baseClass;
157  }
158  }
159  }
160 }
AddBuilder(ConverterCollection converterCollection, ConverterCollectionSettings settings, ref CancellationToken cancellationToken)
Creates a new instance of AddBuilder<TBase> for grouped-adding of converter-functions with mutual arg...
AddBuilder(ConverterCollection converterCollection, ConverterCollectionSettings settings=null, CancellationToken cancellationToken=default(CancellationToken))
Creates a new instance of AddBuilder<TBase> for grouped-adding of converter-functions with default ar...
IConverterCollection Cancel()
Dummy function to explicitly end the current chained builder operation without applying any deferred ...
The thread-safe, static collection of Converter items, using Core.Singleton and supporting Core...
The settings for the ConverterCollection.
ConverterCollectionSettings Settings
The settings for the ConverterCollection.
Allows to perform a deferred adding operation of multiple adds using a common Base-Class Type argumen...
IConverterCollection End()
End the deferred adding operation by invocation the deferred list of actions