9 namespace Core.TypeCast
13 using System.Reflection;
20 public static class ConverterExtension
42 public static Func<TIn, TOut, TOut> FunctionDefaultWrapper<TIn, TOut>(
this Converter
self)
44 if (
self.Function == null)
48 Func<TIn, TOut> converter = (Func<TIn, TOut>)
self.Function;
49 Func<TIn, TOut, TOut> converterDefault = (value, defaultValue) =>
51 TOut retvalue =
default(TOut);
54 var result = converter(value);
55 retvalue = Equals(
default(TOut), result) ==
true ? defaultValue : result;
59 throw new ConverterException(
ConverterCause.ConverterWrapperError, exc);
63 return converterDefault;
76 public static Func<TIn, TOut, TOut> FunctionDefaultWrapper<TIn, TOut>(
this Converter<TIn, TOut>
self)
78 return (
self as Converter)?.FunctionDefaultWrapper<TIn, TOut>();
89 public static bool MergeStandard(
this Converter
self, Converter converter)
92 if(
self != null &&
self.Standard ==
true)
94 if((
self.HasFunction == converter.HasFunction) && (
self.HasDefaultFunction == converter.HasDefaultFunction))
100 if(
self.HasDefaultFunction ==
false)
102 self.FunctionDefault = converter.FunctionDefault;
106 self.Function = converter.Function;
109 self.Attribute =
self.Attribute ?? converter.Attribute;
121 public static bool SetCollectionDefaults(
this Converter
self, ConverterCollection collection = null)
123 if(collection != null)
126 self.Collection = collection;
129 if(
self.Collection == null)
132 self.DefaultValueAnyType |=
self.Collection.Settings.DefaultValueAnyType;
133 self.UseFunctionDefaultWrapper |=
self.Collection.Settings.UseFunctionDefaultWrapper;
144 public static void SetBaseType(
this Converter
self, Type baseType)
146 if(
self.BaseType == null && baseType != null)
148 self.BaseType = baseType.GetTypeInfo();
150 else if(
self.BaseType == null && baseType == null &&
self.HasFunction)
152 self.BaseType = ((
self.Function ??
self.FunctionDefault) as Delegate)?
165 public static IQueryable<Converter> WithSameFromType(
this Converter
self)
167 if(
self.Collection != null)
169 return self.Collection.Items.AsQueryable().WithFrom(
self.From);
179 public static IQueryable<Converter> WithSameToType(
this Converter
self)
181 if(
self.Collection != null)
183 return self.Collection.Items.AsQueryable().WithTo(
self.To);
196 public static void MergeFromMethodAttribute(
this Converter
self, MethodInfo methodInfo)
198 var classAttribute = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttribute<ConverterAttribute>();
199 var methodAttribute = methodInfo.GetCustomAttribute<ConverterMethodAttribute>();
202 self.Attribute =
new ConverterAttribute(loadOnDemand: classAttribute?.LoadOnDemand ??
false,
203 name: String.IsNullOrWhiteSpace(classAttribute?.Name) ? methodAttribute?.Name : classAttribute.Name,
204 nameSpace: methodInfo.DeclaringType.Namespace,
205 dependencyInjection: classAttribute?.DependencInjection ??
false);
208 if(methodAttribute?.BaseType != null)
210 self.BaseType =
self.BaseType ?? methodAttribute.BaseType.GetTypeInfo();
ConverterCollectionCause
Contains the reasons for a ConverterCollectionException to be raised.
ConverterCause
Contains the reasons for a ConverterException to be raised.