.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
ConverterFactoryRT.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 Core.TypeCast;
13  using System.Diagnostics;
14  using System.Reflection;
15  using System.Runtime.Serialization;
16 
22  public class ConverterFactoryRT<TConverter> : Factory<Converter, MethodInfo, object> where TConverter : class
23  {
24  public override Converter Create(MethodInfo method)
25  {
26  return this.Create(method, null);
27  }
28 
39  public override Converter Create(MethodInfo methodInfo, object converterDelegate = null)
40  {
41  if(methodInfo == null && converterDelegate == null)
42  {
43  throw new ConverterException(ConverterCause.ConverterArgumentNull);
44  }
45 
46  var parameterInfos = methodInfo.GetParameters();
47 
48  if(parameterInfos.Length == 0)
49  {
50  throw new ConverterException(ConverterCause.ConverterArgumentDelegateNoParameters);
51  }
52 
53  bool isInstanceMethod = converterDelegate == null;
54 
55  var parameterTypes = new ConverterParameters(methodInfo.ReturnParameter, parameterInfos);
56 
57  Converter converter;
58 
59  var methodAttribute = methodInfo.GetCustomAttribute<ConverterMethodAttribute>();
60 
61  // create either a Converter_T2 or Converter_T3 container
62  if(parameterInfos.Length == 2 || isInstanceMethod == true)
63  {
64  converter = Instantiate<Converter>(typeof(Converter<,,>), parameters: parameterTypes.ToArray(), args: new[] { converterDelegate ?? methodInfo, parameterTypes.Arg });
65 
66  converter.FunctionDefaultAttribute = methodAttribute;
67  converter.FunctionAttribute = methodAttribute;
68  }
69  else if(parameterInfos.Length == 1)
70  {
71  converter = Instantiate<Converter>(typeof(Converter<,>), parameters: new[] { parameterTypes.In, parameterTypes.Out }, args: converterDelegate);
72  converter.FunctionAttribute = methodAttribute;
73  }
74  else
75  {
76  throw new ConverterException(ConverterCause.ConverterArgumentDelegateTooManyParameters);
77  }
78 
79  converter.MergeFromMethodAttribute(methodInfo);
80 
81  return converter;
82  }
83  }
84 }
ConverterMethodAttribute FunctionAttribute
Gets an instance of ConverterMethodAttribute of the Function if it exists. May also be null if the at...
Definition: Converter.cs:126
ConverterMethodAttribute FunctionDefaultAttribute
Gets an instance of ConverterMethodAttribute of the FunctionDefault if it exists. May also be null if...
Definition: Converter.cs:149
Use ConverterMethodAttribute to declare a method in an arbitrary class as a logical Converter functio...
The Exception-type which is raised exclusively by the Converter<TIn,TOut> Library ...
Creates new instances of Converter<TIn, TOut> or Converter<TIn, TOut, TArg> dependent on the number o...
The abstract generic factory for creating arbitrary instances requiring up to two arguments...
Definition: Factory.cs:30
override Converter Create(MethodInfo methodInfo, object converterDelegate=null)
Creates a strictly typed Converter<TIn, TOut, TArg> container in case of any attribute-assigned metho...
Container with sequentially assigned type-parameters of a strictly typed Converter-Function, in the sequence of Types for: In, Out, Argument
override Converter Create(MethodInfo method)
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