-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAdditionalInterface.cs
75 lines (69 loc) · 3.19 KB
/
AdditionalInterface.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Globalization;
using Unity.Interception.ContainerIntegration.ObjectBuilder;
using Unity.Interception.Properties;
using Unity.Policy;
namespace Unity.Interception.ContainerIntegration
{
/// <summary>
/// Stores information about a single <see cref="Type"/> to be an additional interface for an intercepted object and
/// configures a container accordingly.
/// </summary>
public class AdditionalInterface : InterceptionMember
{
private readonly Type _additionalInterface;
/// <summary>
/// Initializes a new instance of the <see cref="AdditionalInterface"/> with a
/// <see cref="Type"/>.
/// </summary>
/// <param name="additionalInterface">A descriptor representing the interception behavior to use.</param>
/// <exception cref="ArgumentNullException">when <paramref name="additionalInterface"/> is
/// <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">when <paramref name="additionalInterface"/> is not an interface.
/// </exception>
public AdditionalInterface(Type additionalInterface)
{
if (!(additionalInterface ?? throw new ArgumentNullException(nameof(additionalInterface))).IsInterface)
{
throw new ArgumentException(
string.Format(
CultureInfo.CurrentCulture,
Resources.ExceptionTypeIsNotInterface,
additionalInterface.Name),
nameof(additionalInterface));
}
_additionalInterface = additionalInterface;
}
/// <summary>
/// Add policies to the <paramref name="policies"/> to configure the container to use the represented
/// <see cref="Type"/> as an additional interface for the supplied parameters.
/// </summary>
/// <param name="serviceType">Interface being registered.</param>
/// <param name="implementationType">Type to register.</param>
/// <param name="name">Name used to resolve the type object.</param>
/// <param name="policies">Policy list to add policies to.</param>
public override void AddPolicies(Type serviceType, Type implementationType, string name, IPolicyList policies)
{
AdditionalInterfacesPolicy policy =
AdditionalInterfacesPolicy.GetOrCreate(policies, implementationType, name);
policy.AddAdditionalInterface(_additionalInterface);
}
}
/// <summary>
/// Stores information about a single <see cref="Type"/> to be an additional interface for an intercepted object and
/// configures a container accordingly.
/// </summary>
/// <typeparam name="T">The interface.</typeparam>
public class AdditionalInterface<T> : AdditionalInterface
where T : class
{
/// <summary>
/// Initializes a new instance of the <see cref="AdditionalInterface{T}"/>.
/// </summary>
public AdditionalInterface()
: base(typeof(T))
{
}
}
}