Skip to content

Commit b00eaf4

Browse files
committed
Move common methods to abstract class
1 parent 3935e1f commit b00eaf4

2 files changed

Lines changed: 31 additions & 30 deletions

File tree

src/UnityContainerAttributeRegistration/Populator/Populator.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection;
5+
6+
using JetBrains.Annotations;
47

58
using Unity;
69

@@ -41,5 +44,33 @@ protected IEnumerable<Type> GetTypesWith<TAttribute>(TypeDefined typeDefined) wh
4144
.Where(type => type.IsDefined(typeof(TAttribute), typeDefined == TypeDefined.Inherit))
4245
;
4346
}
47+
48+
/// <summary>
49+
/// Create an instance for <paramref name="objectType" />.
50+
/// </summary>
51+
/// <param name="objectType"><see cref="Type" /> used to create an instance.</param>
52+
/// <typeparam name="T"><paramref name="objectType" /> must be type-equal to, inherit or implement <typeparamref name="T" />.</typeparam>
53+
/// <returns>New instance of <paramref name="objectType" /> as <typeparamref name="T" />.</returns>
54+
/// <exception cref="InvalidOperationException">Cannot create an instance of <paramref name="objectType" />. Whether <paramref name="objectType" /> is not type-equal, does not inherit or implement <typeparamref name="T" /> or has no default constructor.</exception>
55+
[NotNull]
56+
protected T GetInstanceByType<T>([NotNull] Type objectType)
57+
{
58+
Type targetType = typeof(T);
59+
60+
if(!targetType.IsAssignableFrom(objectType))
61+
{
62+
throw new InvalidOperationException($"Type {objectType.FullName} cannot be assigned from {targetType.FullName}");
63+
}
64+
65+
ConstructorInfo ctor = objectType.GetConstructor(Type.EmptyTypes);
66+
67+
if(ctor == null)
68+
{
69+
throw new InvalidOperationException(
70+
$"Cannot create instance of ITypeLifetimeManager. No default constructor found for {objectType.FullName}");
71+
}
72+
73+
return (T) ctor.Invoke(new object[0]);
74+
}
4475
}
4576
}

src/UnityContainerAttributeRegistration/Populator/TypePopulator.cs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using System.Linq;
44
using System.Reflection;
55

6-
using JetBrains.Annotations;
7-
86
using Unity;
97
using Unity.Lifetime;
108

@@ -59,33 +57,5 @@ public override IUnityContainer Populate(IUnityContainer container)
5957

6058
return container;
6159
}
62-
63-
/// <summary>
64-
/// Create an instance for <paramref name="objectType" />.
65-
/// </summary>
66-
/// <param name="objectType"><see cref="Type" /> used to create an instance.</param>
67-
/// <typeparam name="T"><paramref name="objectType" /> must be type-equal to, inherit or implement <typeparamref name="T" />.</typeparam>
68-
/// <returns>New instance of <paramref name="objectType" /> as <typeparamref name="T" />.</returns>
69-
/// <exception cref="InvalidOperationException">Cannot create an instance of <paramref name="objectType" />. Whether <paramref name="objectType" /> is not type-equal, does not inherit or implement <typeparamref name="T" /> or has no default constructor.</exception>
70-
[NotNull]
71-
private T GetInstanceByType<T>([NotNull] Type objectType)
72-
{
73-
Type targetType = typeof(T);
74-
75-
if(!targetType.IsAssignableFrom(objectType))
76-
{
77-
throw new InvalidOperationException($"Type {objectType.FullName} cannot be assigned from {targetType.FullName}");
78-
}
79-
80-
ConstructorInfo ctor = objectType.GetConstructor(Type.EmptyTypes);
81-
82-
if(ctor == null)
83-
{
84-
throw new InvalidOperationException(
85-
$"Cannot create instance of ITypeLifetimeManager. No default constructor found for {objectType.FullName}");
86-
}
87-
88-
return (T) ctor.Invoke(new object[0]);
89-
}
9060
}
9161
}

0 commit comments

Comments
 (0)