.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
Factory.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 using System.Reflection;
10 using System.Linq;
11 using System;
12 
13 using System.Runtime.CompilerServices;
14 using System.Runtime.InteropServices;
15 
16 namespace Core.TypeCast.Base
17 {
18  using System.Collections.Generic;
19  using Extensions;
20 
30  public abstract class Factory<TInstance, TIn1, TIn2> : IFactory<TInstance, TIn1, TIn2> where TInstance : class
31  {
35  public string Name
36  {
37  get
38  {
39  return this.ToString();
40  }
41  }
42 
46  public IEnumerable<Type> GetInterfaces()
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  }
56 
60  public IEnumerable<Type> GetParameters()
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  }
70 
76  public abstract TInstance Create(TIn1 parameter);
77 
85  public abstract TInstance Create(TIn1 parameter, TIn2 parameter2 = default(TIn2));
86 
93  protected static object Instantiate(Type type, params object[] parameters)
94  {
95  return Instantiate<object>(type: type, args: parameters);
96  }
97 
105  protected static bool TryInstantiate(Type type, out object instance, params object[] parameters)
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  }
116 
125  protected static TOut Instantiate<TOut>(Type type, Type[] parameters, params object[] args)
126  {
127  var constructed = type.GetTypeInfo().MakeGenericType(parameters);
128  var instance = Instantiate<TOut>(type: constructed, args: args);
129  return instance;
130  }
131 
141  protected static TOut Instantiate<TOut>(Type type, object[] args = null)
142  {
143  if(args?.Length == 0)
144  {
145  return (TOut)Activator.CreateInstance(type);
146  }
147  return (TOut)Activator.CreateInstance(type, args);
148  }
149 
153  public override string ToString()
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  }
168 
169  }
170 }
override string ToString()
Returns a string representation of the Factory type.
Definition: Factory.cs:153
The generic, common factory interface for declaring factories creating arbitrary object instances req...
Definition: IFactory.cs:20
The abstract generic factory for creating arbitrary instances requiring up to two arguments...
Definition: Factory.cs:30
static object Instantiate(Type type, params object[] parameters)
Internal method for object instantiation by a passed type type
Definition: Factory.cs:93
IEnumerable< Type > GetInterfaces()
Returns an IEnumerable<Type> of the interfaces supported by the factory instance. ...
Definition: Factory.cs:46
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...
Definition: Factory.cs:105
IEnumerable< Type > GetParameters()
Returns an IEnumerable<Type> of the generic parameters that constructed the base-factory instance...
Definition: Factory.cs:60