9 namespace Core.TypeCast
12 using System.Collections.Generic;
14 using System.Reflection;
15 using System.Runtime.CompilerServices;
16 using System.Runtime.InteropServices;
25 public static class ConverterCollectionLookup
35 public static Converter Get(
this IQueryable<Converter> query, IConverter baseTypeInstance)
37 return query.Get(baseTypeInstance?.GetType());
47 public static Converter Get(
this IQueryable<Converter> query, Type baseType)
50 var interfaceType = baseType?.GetTypeInfo()
51 .ImplementedInterfaces.ToList()
52 .FirstOrDefault(c => c.Name.StartsWith(nameof(IConverter)));
53 if(interfaceType != null && interfaceType.IsConstructedGenericType)
55 var types = interfaceType.GenericTypeArguments;
57 var converter = query.Get(types.First().GetTypeInfo(), types.Last().GetTypeInfo());
70 public static Converter Get(
this IQueryable<Converter> query, Type typeFrom, Type typeTo)
72 return query.Get(typeFrom: typeFrom.GetTypeInfo(), typeTo: typeTo.GetTypeInfo());
84 public static Converter Get<TOut>(
this IQueryable<Converter> query,
object value,
bool? hasDefault = null,
bool loadOnDemand =
false)
86 return query.Get(value?.GetType().GetTypeInfo(), typeof(TOut).GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
100 public static Converter Get<TIn, TOut>(
this IQueryable<Converter> query, TIn value =
default(TIn), Type typeArgument = null,
bool? hasDefault = null,
bool loadOnDemand =
false)
102 return query.Get(typeof(TIn).GetTypeInfo(), typeof(TOut).GetTypeInfo(), typeArgument: typeArgument?.GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
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)
121 return query.Get(typeof(TIn).GetTypeInfo(), typeof(TOut).GetTypeInfo(), typeArgument: typeof(TArg).GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
134 public static Converter Get<TOut>(
this IQueryable<Converter> query, Type typeFrom, Type typeArgument = null,
bool? hasDefault = null,
bool loadOnDemand =
false)
136 return query.Get(typeFrom.GetTypeInfo(), typeof(TOut).GetTypeInfo(), typeArgument: typeArgument?.GetTypeInfo(), hasDefaultFunction: hasDefault, loadOnDemand: loadOnDemand);
162 public static Converter Get(
this IQueryable<Converter> query,
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)
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);
182 if(loadOnDemand ==
true && query.Any() ==
false && ((query as ConverterCollection) ?? ConverterCollection.CurrentInstance)?.LoadOnDemandConverter(typeTo?.AsType()) > 0)
184 query = query.WithFrom(typeFrom).WithTo(typeTo);
188 if(query.Count() > 1 && typeArgument == null)
190 query = query.WithArgument(typeof(
object), assignable:
false);
193 return query.FirstOrDefault();