.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
ConverterCollectionLookup.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.Generic;
13  using System.Linq;
14  using System.Reflection;
15  using System.Runtime.CompilerServices;
16  using System.Runtime.InteropServices;
17 
18  using Core.Extensions;
19  using Core.Singleton;
20  using Core.TypeCast.Base;
21 
25  public static class ConverterCollectionLookup
26  {
27 
35  public static Converter Get(this IQueryable<Converter> query, IConverter baseTypeInstance)
36  {
37  return query.Get(baseTypeInstance?.GetType());
38  }
39 
47  public static Converter Get(this IQueryable<Converter> query, Type baseType)
48  {
49 
50  var interfaceType = baseType?.GetTypeInfo()
51  .ImplementedInterfaces.ToList()
52  .FirstOrDefault(c => c.Name.StartsWith(nameof(IConverter)));
53  if(interfaceType != null && interfaceType.IsConstructedGenericType)
54  {
55  var types = interfaceType.GenericTypeArguments;
56 
57  var converter = query.Get(types.First().GetTypeInfo(), types.Last().GetTypeInfo());
58  return converter;
59  }
60  return null;
61  }
62 
70  public static Converter Get(this IQueryable<Converter> query, Type typeFrom, Type typeTo)
71  {
72  return query.Get(typeFrom: typeFrom.GetTypeInfo(), typeTo: typeTo.GetTypeInfo());
73  }
74 
84  public static Converter Get<TOut>(this IQueryable<Converter> query, object value, bool? hasDefault = null, bool loadOnDemand = false)
85  {
86  return query.Get(value?.GetType().GetTypeInfo(), typeof(TOut).GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
87  }
88 
100  public static Converter Get<TIn, TOut>(this IQueryable<Converter> query, TIn value = default(TIn), Type typeArgument = null, bool? hasDefault = null, bool loadOnDemand = false)
101  {
102  return query.Get(typeof(TIn).GetTypeInfo(), typeof(TOut).GetTypeInfo(), typeArgument: typeArgument?.GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
103  }
104 
119  public static Converter Get<TIn, TArg, TOut>(this IQueryable<Converter> query, TIn value = default(TIn), TArg argument = default(TArg), TOut defaultValue = default(TOut), bool? hasDefault = null, bool loadOnDemand = false)
120  {
121  return query.Get(typeof(TIn).GetTypeInfo(), typeof(TOut).GetTypeInfo(), typeArgument: typeof(TArg).GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
122  }
123 
134  public static Converter Get<TOut>(this IQueryable<Converter> query, Type typeFrom, Type typeArgument = null, bool? hasDefault = null, bool loadOnDemand = false)
135  {
136  return query.Get(typeFrom.GetTypeInfo(), typeof(TOut).GetTypeInfo(), typeArgument: typeArgument?.GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
137  }
138 
162  public static Converter Get(this IQueryable<Converter> query,
163  TypeInfo typeFrom,
164  TypeInfo typeTo,
165  TypeInfo typeArgument = null,
166  TypeInfo typeBase = null,
167  bool? hasDefaultFunction = null,
168  bool loadOnDemand = false,
169  bool? isStandard = null,
170  bool? typeFromIsGenericType = null,
171  bool? typeToIsGenericType = null,
172  string functionName = null,
173  string attributeName = null,
174  bool assignable = false,
175  bool withContext = false)
176  {
177  query = query.ApplyAllFilters(typeFrom: typeFrom, typeTo: typeTo, typeArgument: typeArgument, typeBase: typeBase, hasDefaultFunction: hasDefaultFunction,
178  isStandard: isStandard, typeFromIsGenericType: typeFromIsGenericType, typeToIsGenericType: typeToIsGenericType,
179  functionName: functionName, attributeName: attributeName, assignableFrom: assignable, assignableTo: assignable,
180  assignableArgument: assignable, withContext: withContext);
181 
182  if(loadOnDemand == true && query.Any() == false && ((query as ConverterCollection) ?? ConverterCollection.CurrentInstance)?.LoadOnDemandConverter(typeTo?.AsType()) > 0)
183  {
184  query = query.WithFrom(typeFrom).WithTo(typeTo);
185  }
186 
187  // with disambiguates and no specified type-argument, pick the most generic converter
188  if(query.Count() > 1 && typeArgument == null)
189  {
190  query = query.WithArgument(typeof(object), assignable: false);
191  }
192 
193  return query.FirstOrDefault();
194  }
195 
196  }
197 
198 }