.NET Portable TypeCast  3.1.0.4
A, easy-to-use tested, generic, portable, runtime-extensible, arbitrary type converter library
BaseClassFactoryRT.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 Core.Extensions;
14  using System.Diagnostics;
15  using System.Reflection;
16  using System.Runtime.Serialization;
17 
21  [System.Runtime.InteropServices.ComVisible(false)]
22  public class BaseClassFactoryRT : Factory<object, Type, ConverterCollection>
23  {
32  public override object Create(Type converterClass)
33  {
34  return this.Create(converterClass, ConverterCollection.CurrentInstance);
35  }
36 
47  public override object Create(Type converterClass, ConverterCollection collection = null)
48  {
49  if(collection?.ConverterClassInitialized.ContainsKey(converterClass.GetTypeInfo()) == true)
50  {
51  if(collection?.Settings.ConverterClassExistsException == true)
52  {
53  throw new ConverterCollectionException(ConverterCollectionCause.ConverterClassExists);
54  }
55  return null;
56  }
57 
58  object customConverter;
59  try
60  {
61  if(typeof(ConverterCollectionDependency).GetTypeInfo().IsAssignableFrom(converterClass.GetTypeInfo())
62  || converterClass.GetTypeInfo().IsDependencyInjectable(typeof(IConverterCollection)))
63  {
64  customConverter = Instantiate(type: converterClass, parameters: collection as IConverterCollection);
65  }
66  else
67  {
68  customConverter = Instantiate(type: converterClass);
69  }
70  }
71  catch(TargetInvocationException exc)
72  {
73  throw exc?.InnerException;
74  }
75 
76  return customConverter;
77  }
78  }
79 }
Creates instances from classes which are attributed by ConverterAttribute
The thread-safe, static collection of Converter items, using Core.Singleton and supporting Core...
The converter dependency provides loose implementation of Converters, merely constricting the constru...
override object Create(Type converterClass, ConverterCollection collection=null)
Creates a new instance of Type converterClass
ConverterCollectionCause
Contains the reasons for a ConverterCollectionException to be raised.
The abstract generic factory for creating arbitrary instances requiring up to two arguments...
Definition: Factory.cs:30
The Exception-type which is raised exclusively by the Converter<T> Library
override object Create(Type converterClass)
Creates a new instance of Type converterClass