.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
Core.TypeCast.Base.ConverterFactoryRT< TConverter > Class Template Referenceabstract

Creates new instances of Converter<TIn, TOut> or Converter<TIn, TOut, TArg> dependent on the number of parameters defined in methodInfo and whether a delegate was passed. More...

+ Collaboration diagram for Core.TypeCast.Base.ConverterFactoryRT< TConverter >:

Public Member Functions

override Converter Create (MethodInfo method)
 
override Converter Create (MethodInfo methodInfo, object converterDelegate=null)
 Creates a strictly typed Converter<TIn, TOut, TArg> container in case of any attribute-assigned method or a passed MethodInfo with a parameter argument count of two. Otherwise a strictly typed Converter<TIn, TOut> container instance is created and returned. More...
 
IEnumerable< Type > GetInterfaces ()
 Returns an IEnumerable<Type> of the interfaces supported by the factory instance. More...
 
IEnumerable< Type > GetParameters ()
 Returns an IEnumerable<Type> of the generic parameters that constructed the base-factory instance. More...
 
abstract TInstance Create (TIn1 parameter)
 Abstract method for creating instances of TInstance defined only through parameter More...
 
abstract TInstance Create (TIn1 parameter, TIn2 parameter2=default(TIn2))
 Abstract method for creating instances of TInstance defined only through parameter and parameter2 More...
 
override string ToString ()
 Returns a string representation of the Factory type. More...
 

Static Protected Member Functions

static object Instantiate (Type type, params object[] parameters)
 Internal method for object instantiation by a passed type type More...
 
static bool TryInstantiate (Type type, out object instance, params object[] parameters)
 Internal method for object instantiation following the "Try" convention of returning a bool true upon success and passing the result with out More...
 
static TOut Instantiate< TOut > (Type type, Type[] parameters, params object[] args)
 Internal method object instantiation of a generic type , with the generic-parameters passed as the second argument More...
 
static TOut Instantiate< TOut > (Type type, object[] args=null)
 Creates an instance of the type designated by the specified generic type parameter More...
 

Properties

string Name [get]
 Gets a string representation of the Base-Factory using ToString() More...
 

Detailed Description

Creates new instances of Converter<TIn, TOut> or Converter<TIn, TOut, TArg> dependent on the number of parameters defined in methodInfo and whether a delegate was passed.

Template Parameters
TConverterThe generic type of the Converter class to instance
Type Constraints
TConverter :class 

Definition at line 22 of file ConverterFactoryRT.cs.

Member Function Documentation

override Converter Core.TypeCast.Base.ConverterFactoryRT< TConverter >.Create ( MethodInfo  method)

Definition at line 24 of file ConverterFactoryRT.cs.

25  {
26  return this.Create(method, null);
27  }
override Converter Create(MethodInfo method)
override Converter Core.TypeCast.Base.ConverterFactoryRT< TConverter >.Create ( MethodInfo  methodInfo,
object  converterDelegate = null 
)

Creates a strictly typed Converter<TIn, TOut, TArg> container in case of any attribute-assigned method or a passed MethodInfo with a parameter argument count of two. Otherwise a strictly typed Converter<TIn, TOut> container instance is created and returned.

Parameters
methodInfoA methodInfo for a converter method, crucially confining the number of function parameters between one and two. Otherwise an ConverterException is raised.
converterDelegateAn optional delegate of the converter method.
Returns
A new instance of a Converter with a parent of either Converter<TIn, TOut, TArg> or Converter<TIn, TOut>.
Exceptions
ConverterExceptionIf the number or types of the parameters mismatch an exception is raised.

Definition at line 39 of file ConverterFactoryRT.cs.

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  }
ConverterCause
Contains the reasons for a ConverterException to be raised.
abstract TInstance Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Create ( TIn1  parameter)
pure virtualinherited

Abstract method for creating instances of TInstance defined only through parameter

Parameters
parameterThe parameter to define the instance creation process
Returns
an instance of Type TInstance
abstract TInstance Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Create ( TIn1  parameter,
TIn2  parameter2 = default(TIn2) 
)
pure virtualinherited

Abstract method for creating instances of TInstance defined only through parameter and parameter2

Parameters
parameterThe parameter to define the instance creation process
parameter2The 2. parameter to define the instance creation process
Returns
an instance of Type TInstance
IEnumerable<Type> Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.GetInterfaces ( )
inherited

Returns an IEnumerable<Type> of the interfaces supported by the factory instance.

Definition at line 46 of file Factory.cs.

47  {
48  var type = this.GetType();
49  yield return type;
50 
51  foreach(var interfaceType in type.GetTypeInfo().ImplementedInterfaces)
52  {
53  yield return interfaceType;
54  }
55  }
IEnumerable<Type> Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.GetParameters ( )
inherited

Returns an IEnumerable<Type> of the generic parameters that constructed the base-factory instance.

Definition at line 60 of file Factory.cs.

61  {
62  var type = this.GetType();
63  yield return type;
64 
65  foreach(var parameterType in type.GetTypeInfo().GenericTypeParameters)
66  {
67  yield return parameterType;
68  }
69  }
static object Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Instantiate ( Type  type,
params object[]  parameters 
)
staticprotectedinherited

Internal method for object instantiation by a passed type type

Parameters
typethe type of the class or struct which to instance
parametersthe parameters passed to the constructor of the class or struct
Returns
Returns an instance object of type TOut or null

Definition at line 93 of file Factory.cs.

94  {
95  return Instantiate<object>(type: type, args: parameters);
96  }
static TOut Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Instantiate< TOut > ( Type  type,
Type[]  parameters,
params object[]  args 
)
staticprotectedinherited

Internal method object instantiation of a generic type , with the generic-parameters passed as the second argument

Template Parameters
TOutThe type of the instance to pass out
Parameters
typeThe type of the class or struct which to instance
parametersthe generic types that comprise the generic type
argsThe parameters passed to the constructor of the class or struct
Returns
Returns an instance object of type TOut or null

Definition at line 125 of file Factory.cs.

126  {
127  var constructed = type.GetTypeInfo().MakeGenericType(parameters);
128  var instance = Instantiate<TOut>(type: constructed, args: args);
129  return instance;
130  }
static TOut Instantiate< TOut >(Type type, Type[] parameters, params object[] args)
Internal method object instantiation of a generic type , with the generic-parameters passed as the se...
Definition: Factory.cs:125
static TOut Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Instantiate< TOut > ( Type  type,
object[]  args = null 
)
staticprotectedinherited

Creates an instance of the type designated by the specified generic type parameter

Parameters
typeThe type of the class or struct which to instance
argsthe parameters passed to the constructor of the class or struct
Returns
A reference to the newly created object.
Exceptions
System.MissingMethodExceptionNoteIn the .NET for Windows Store apps or the Portable Class Library, catch the base class exception, System.MissingMemberException, instead. The type that specified for T does not have a parameterless constructor.

Centralize all runtime calls to

<tt>Activator.Create(...)</tt>

and

constructor.Invoke(...)`

Definition at line 141 of file Factory.cs.

142  {
143  if(args?.Length == 0)
144  {
145  return (TOut)Activator.CreateInstance(type);
146  }
147  return (TOut)Activator.CreateInstance(type, args);
148  }
override string Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.ToString ( )
inherited

Returns a string representation of the Factory type.

Definition at line 153 of file Factory.cs.

154  {
155  var type = this.GetType().GetTypeInfo();
156  var genericArgs = type.GenericTypeArguments;
157  if(genericArgs.Any())
158  {
159  var typeNames = genericArgs.Select(t => t.Name)
160  .Aggregate((a, b) => a + ',' + b);
161  return $"{type.Name} <{typeNames}>";
162  }
163  else
164  {
165  return type.Name;
166  }
167  }
static bool Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.TryInstantiate ( Type  type,
out object  instance,
params object[]  parameters 
)
staticprotectedinherited

Internal method for object instantiation following the "Try" convention of returning a bool true upon success and passing the result with out

Parameters
typeThe type of the class or struct which to instance
parametersThe parameters passed to the constructor of the class or struct
instanceThe assigned instance reference upon instancing of type TOut or null upon failure
Returns
Returns bool true upon success or false upon failure and assigning null to instance

Definition at line 105 of file Factory.cs.

106  {
107  instance = null;
108  try {
109  instance = Instantiate<object>(type: type, args: parameters);
110  } catch(Exception)
111  {
112  return false;
113  }
114  return true;
115  }

Property Documentation

string Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Name
getinherited

Gets a string representation of the Base-Factory using ToString()

Definition at line 36 of file Factory.cs.


The documentation for this class was generated from the following file: