13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
16 namespace Core.TypeCast.Base
18 using System.Collections.Generic;
30 public abstract class Factory<TInstance, TIn1, TIn2> :
IFactory<TInstance, TIn1, TIn2> where TInstance : class
39 return this.ToString();
48 var type = this.GetType();
51 foreach(var interfaceType
in type.GetTypeInfo().ImplementedInterfaces)
53 yield
return interfaceType;
62 var type = this.GetType();
65 foreach(var parameterType
in type.GetTypeInfo().GenericTypeParameters)
67 yield
return parameterType;
76 public abstract TInstance Create(TIn1 parameter);
85 public abstract TInstance Create(TIn1 parameter, TIn2 parameter2 =
default(TIn2));
93 protected static object Instantiate(Type type, params
object[] parameters)
95 return Instantiate<object>(type: type, args: parameters);
105 protected static bool TryInstantiate(Type type, out
object instance, params
object[] parameters)
109 instance = Instantiate<object>(type: type, args: parameters);
125 protected static TOut Instantiate<TOut>(Type type, Type[] parameters, params
object[] args)
127 var constructed = type.GetTypeInfo().MakeGenericType(parameters);
128 var instance = Instantiate<TOut>(type: constructed, args: args);
141 protected static TOut Instantiate<TOut>(Type type,
object[] args = null)
143 if(args?.Length == 0)
145 return (TOut)Activator.CreateInstance(type);
147 return (TOut)Activator.CreateInstance(type, args);
155 var type = this.GetType().GetTypeInfo();
156 var genericArgs = type.GenericTypeArguments;
157 if(genericArgs.Any())
159 var typeNames = genericArgs.Select(t => t.Name)
160 .Aggregate((a, b) => a +
',' + b);
161 return $
"{type.Name} <{typeNames}>";
override string ToString()
Returns a string representation of the Factory type.
The generic, common factory interface for declaring factories creating arbitrary object instances req...
The abstract generic factory for creating arbitrary instances requiring up to two arguments...
static object Instantiate(Type type, params object[] parameters)
Internal method for object instantiation by a passed type type
IEnumerable< Type > GetInterfaces()
Returns an IEnumerable<Type> of the interfaces supported by the factory instance. ...
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...
IEnumerable< Type > GetParameters()
Returns an IEnumerable<Type> of the generic parameters that constructed the base-factory instance...