9 namespace Core.TypeCast
12 using System.Reflection;
13 using System.Reflection.Emit;
14 using System.Runtime.Serialization;
35 : base(typeof(TIn), typeof(TOut), typeof(TArg))
43 this.ConverterFunc = converter;
55 public Converter(Func<TIn, TOut, TOut> converterDefault)
56 : base(typeof(TIn), typeof(TOut), typeof(TArg))
58 if(converterDefault == null)
63 this.ConverterDefaultFunc = converterDefault;
76 public Converter(Func<TIn, TArg, TOut> converterAny, Type argument = null)
77 : base(typeof(TIn), typeof(TOut), argument ?? typeof(TArg))
79 if(converterAny == null)
84 this.DefaultValueAnyType =
true;
85 this.ConverterAnyFunc = converterAny;
98 public Converter(MethodInfo converterInfo, Type argument = null)
99 : base(typeof(TIn), typeof(TOut), argument ?? typeof(TArg))
101 var parameterCount = converterInfo.GetParameters().Length;
103 if(converterInfo == null || parameterCount == 0)
108 if(parameterCount == 1)
110 this.FunctionInfo = converterInfo;
111 this.ConverterFunc = (Func<TIn, TOut>)converterInfo.CreateDelegate(typeof(Func<TIn, TOut>), null);
115 else if(parameterCount == 2)
117 this.FunctionDefaultInfo = converterInfo;
118 this.DefaultValueAnyType =
true;
119 this.ConverterAnyFunc = (Func<TIn, TArg, TOut>)converterInfo.CreateDelegate(typeof(Func<TIn, TArg, TOut>), null);
133 public Func<TIn, TArg, TOut> ConverterAnyFunc
137 return (Func<TIn, TArg, TOut>)this.FunctionDefault;
142 this.FunctionDefault = (object)value;
150 public Func<TIn, TOut, TOut> ConverterDefaultFunc
154 return (Func<TIn, TOut, TOut>)this.FunctionDefault;
159 this.FunctionDefault = (object)value;
169 public Func<TIn, TOut> ConverterFunc
173 return (Func<TIn, TOut>)this.Function;
178 this.Function = (object)value;
189 public override object Convert(
object value,
object defaultValue = null)
191 this.CheckConvertTypes(value, defaultValue);
195 if(this.HasDefaultFunctionOnly ==
true || (defaultValue != null && ObjectExtension.IsDefaultValue<TOut>(defaultValue) ==
false))
197 return this.ConvertDefault(ref value, ref defaultValue);
200 if(this.ConverterFunc != null)
202 if(this.Base != null && this.FunctionAttribute?.IsStatic ==
false && this.FunctionInfo?.IsStatic ==
false)
204 return this.FunctionInfo.Invoke(this.Base,
new[] { value });
207 return this.ConverterFunc.Invoke((TIn)value);
210 catch(InvalidCastException)
229 private object ConvertDefault(ref
object value, ref
object defaultValue)
231 if(this.HasDefaultFunction ==
true)
234 if(this.Base != null && this.FunctionDefaultAttribute?.IsStatic ==
false && this.FunctionDefaultInfo?.IsStatic ==
false)
236 return this.FunctionDefaultInfo.Invoke(this.Base,
new[] { value, defaultValue });
238 else if(this.DefaultValueAnyType ==
true)
240 return this.ConverterAnyFunc.Invoke((TIn)value, (TArg)(defaultValue ??
default(TArg)));
242 return this.ConverterDefaultFunc.Invoke((TIn)value, (TOut)defaultValue);
244 else if(this.UseFunctionDefaultWrapper ==
true && this.ConverterFunc != null)
249 Func<TIn, TOut, TOut> defaultWrapper = this.FunctionDefaultWrapper<TIn, TOut>();
250 return (TOut)defaultWrapper((TIn)value, (TOut)defaultValue);
257 throw new ConverterException(
ConverterCause.ConverterFunctionDefaultNull,
"A default value exists but no conversion function taking a default-value is defined");
270 public TOut
Convert(TIn valueTyped, TOut defaultValueTyped =
default(TOut))
272 return (TOut)this.Convert(value: valueTyped, defaultValue: defaultValueTyped);
286 if((value is TIn) ==
false)
291 if(defaultValue != null && this.DefaultValueAnyType ==
false && (defaultValue is TOut) ==
false)
Converter(Func< TIn, TOut > converter)
Initializes a new instance of the Converter<TIn, TOut, TArg> class.
The Exception-type which is raised exclusively by the Converter<TIn,TOut> Library ...
The Base Converter interface.
Converter(MethodInfo converterInfo, Type argument=null)
Initializes a new instance of the Converter<TIn, TOut, TArg> class.
TOut Convert(TIn valueTyped, TOut defaultValueTyped=default(TOut))
The converter function that needs to be overwritten as part of the IConverter interface support...
Converter(Func< TIn, TArg, TOut > converterAny, Type argument=null)
Initializes a new instance of the Converter<TIn, TOut, TArg> class.
Converter(Func< TIn, TOut, TOut > converterDefault)
Initializes a new instance of the Converter<TIn, TOut, TArg> class.
override void CheckConvertTypes(object value, object defaultValue)
Checks the types being in the correct source and target format, if not exceptions are thrown...
ConverterCause
Contains the reasons for a ConverterException to be raised.
override object Convert(object value, object defaultValue=null)
The Converter convert function as part of the IConverter interface support.
The Converter base class, providing a simple container for conversion types, ConverterAttribute and c...