.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
ConverterFactory.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.Base
10 {
11  using System;
12  using System.Linq;
13  using Core.TypeCast;
14  using System.Diagnostics;
15  using System.Reflection;
16  using System.Runtime.Serialization;
17 
21  [System.Runtime.InteropServices.ComVisible(false)]
22  public class ConverterFactory : ConverterFactory<Converter>
23  {
32  public override Converter Create<TIn, TOut>(Func<TIn, TOut> method)
33  {
34  var converter = new Converter<TIn, TOut>(method);
35  converter.MergeFromMethodAttribute(method.GetMethodInfo());
36 
37  return converter;
38  }
39 
48  public override Converter Create<TIn, TOut>(Func<TIn, TOut, TOut> method)
49  {
50  var converter = new Converter<TIn, TOut>(method);
51  converter.MergeFromMethodAttribute(method.GetMethodInfo());
52 
53  return converter;
54  }
55 
63  public Converter CreateWrapper(TypeInfo type, MethodInfo declaredMethod)
64  {
65  MethodInfo makeConverter;
66  if(declaredMethod.GetParameters().Length == 0)
67  {
68  makeConverter = typeof(ConverterFactory).GetTypeInfo()
69  .GetDeclaredMethod(nameof(ConverterFactory.ConverterWrapper))
70  .MakeGenericMethod(type.AsType(), declaredMethod.ReturnParameter.ParameterType);
71  }
72  else if(declaredMethod.GetParameters().Length == 1)
73  {
74  makeConverter = typeof(ConverterFactory).GetTypeInfo()
75  .GetDeclaredMethod(nameof(ConverterFactory.ConverterWrapperAny))
76  .MakeGenericMethod(type.AsType(), declaredMethod.GetParameters().FirstOrDefault().ParameterType, declaredMethod.ReturnParameter.ParameterType);
77  }
78  else
79  {
80  throw new ConverterException(ConverterCause.ConverterArgumentDelegateTooManyParameters);
81  }
82  var args = new object[] { declaredMethod }; //: new object[] { declaredMethod }.Concat(parameters).ToArray();
83 
84  var converter = (Converter)makeConverter.Invoke(this, args);
85 
86  converter.MergeFromMethodAttribute(declaredMethod);
87 
88  return converter;
89  }
90 
98  public Converter ConverterWrapper<TClass, TOut>(MethodInfo declaredMethod)
99  {
100  return this.Create<TClass, TOut>(InvocationWrapper<TClass, TOut>(declaredMethod));
101  }
102 
112  public static Converter ConverterWrapperAny<TClass, TArg, TOut>(MethodInfo declaredMethod)
113  {
114  return new Converter<TClass, TOut, TArg>(InvocationWrapper<TClass, TArg, TOut>(declaredMethod));
115  }
116 
124  private static Func<TClass, TOut> InvocationWrapper<TClass, TOut>(MethodInfo declaredMethod)
125  {
126  return (instance) => (TOut)declaredMethod.Invoke(instance, null);
127  }
128 
138  private static Func<TClass, TArg, TOut> InvocationWrapper<TClass, TArg, TOut>(MethodInfo declaredMethod)
139  {
140  return (instance, parameter) => (TOut)declaredMethod.Invoke(instance, new object[] { parameter });
141  }
142  }
143 }
Converter CreateWrapper(TypeInfo type, MethodInfo declaredMethod)
Create a converter-wrapper which passes the own class-instance of a given method declaredMethod as f...
The Exception-type which is raised exclusively by the Converter<TIn,TOut> Library ...
Creates new instances of Converter<TIn, TOut> dependent on the source TIn and target Type TOut ...
ConverterCause
Contains the reasons for a ConverterException to be raised.
The Converter base class, providing a simple container for conversion types, ConverterAttribute and c...
Definition: Converter.cs:22