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

The abstract generic factory for creating arbitrary instances requiring up to two arguments. Use a container type such as Tuple or struct as second parameter type TIn2 if more parameters are required. More...

+ Collaboration diagram for Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >:

Public Member Functions

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

The abstract generic factory for creating arbitrary instances requiring up to two arguments. Use a container type such as Tuple or struct as second parameter type TIn2 if more parameters are required.

Template Parameters
TInstanceThe Type of the instances to create and return by the factory method Create(TIn1) and Create(TIn1, TIn2).
TIn1The parameter type used for defining the instance creation process in the factory method Create(TIn1)
TIn2The 2. parameter type used for defining the instance creation process in the factory method Create(TIn1) and Create(TIn1, TIn2)
Type Constraints
TInstance :class 

Definition at line 30 of file Factory.cs.

Member Function Documentation

abstract TInstance Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Create ( TIn1  parameter)
pure virtual

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

+ Here is the caller graph for this function:

abstract TInstance Core.TypeCast.Base.Factory< TInstance, TIn1, TIn2 >.Create ( TIn1  parameter,
TIn2  parameter2 = default(TIn2) 
)
pure virtual

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 ( )

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 ( )

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 
)
staticprotected

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 
)
staticprotected

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 
)
staticprotected

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 ( )

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 
)
staticprotected

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
get

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: