forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerTypeExtensions.cs
More file actions
77 lines (68 loc) · 3.05 KB
/
ContainerTypeExtensions.cs
File metadata and controls
77 lines (68 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Linq;
using Funq;
using ServiceStack.Text;
namespace ServiceStack
{
public static class ContainerTypeExtensions
{
/// <summary>
/// Registers the type in the IoC container and
/// adds auto-wiring to the specified type.
/// </summary>
/// <param name="serviceType"></param>
/// <param name="inFunqAsType"></param>
public static void RegisterAutoWiredType(this Container container, Type serviceType, Type inFunqAsType,
ReuseScope scope = ReuseScope.None)
{
if (serviceType.IsAbstract || serviceType.ContainsGenericParameters)
return;
var methodInfo = typeof(Container).GetMethod("RegisterAutoWiredAs", Type.EmptyTypes);
var registerMethodInfo = methodInfo.MakeGenericMethod(new[] { serviceType, inFunqAsType });
var registration = registerMethodInfo.Invoke(container, TypeConstants.EmptyObjectArray) as IRegistration;
registration.ReusedWithin(scope);
}
/// <summary>
/// Registers the type in the IoC container and
/// adds auto-wiring to the specified type.
/// The reuse scope is set to none (transient).
/// </summary>
/// <param name="serviceTypes"></param>
public static void RegisterAutoWiredType(this Container container, Type serviceType,
ReuseScope scope = ReuseScope.None)
{
//Don't try to register base service classes
if (serviceType.IsAbstract || serviceType.ContainsGenericParameters)
return;
var methodInfo = typeof(Container).GetMethod("RegisterAutoWired", Type.EmptyTypes);
var registerMethodInfo = methodInfo.MakeGenericMethod(new[] { serviceType });
var registration = registerMethodInfo.Invoke(container, TypeConstants.EmptyObjectArray) as IRegistration;
registration.ReusedWithin(scope);
}
/// <summary>
/// Registers the types in the IoC container and
/// adds auto-wiring to the specified types.
/// The reuse scope is set to none (transient).
/// </summary>
/// <param name="serviceTypes"></param>
public static void RegisterAutoWiredTypes(this Container container, IEnumerable<Type> serviceTypes,
ReuseScope scope = ReuseScope.None)
{
foreach (var serviceType in serviceTypes)
container.RegisterAutoWiredType(serviceType, scope);
}
/// <summary>
/// Register a singleton instance as a runtime type
/// </summary>
public static Container Register(this Container container, object instance, Type asType)
{
var mi = container.GetType()
.GetMethods()
.First(x => x.Name == "Register" && x.GetParameters().Length == 1 && x.ReturnType == typeof(void))
.MakeGenericMethod(asType);
mi.Invoke(container, new[] { instance });
return container;
}
}
}