Added UnityDefaults
This commit is contained in:
Родитель
64f5cc231c
Коммит
53e93fe435
|
@ -1,49 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Unity.Injection;
|
||||
|
||||
namespace Unity.Interception.ContainerIntegration.Selection
|
||||
{
|
||||
/// <summary>
|
||||
/// Objects of this type encapsulate <see cref="ConstructorInfo"/> and resolve
|
||||
/// parameters.
|
||||
/// </summary>
|
||||
public class SelectedConstructor : MethodBase<ConstructorInfo>
|
||||
{
|
||||
/// <summary>
|
||||
/// Create a new <see cref="SelectedConstructor"/> instance which
|
||||
/// contains the given constructor.
|
||||
/// </summary>
|
||||
/// <param name="constructor">The constructor to wrap.</param>
|
||||
public SelectedConstructor(ConstructorInfo constructor)
|
||||
: base(constructor)
|
||||
{
|
||||
}
|
||||
|
||||
public SelectedConstructor(ConstructorInfo info, object[] parameters)
|
||||
: base(info, parameters)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The constructor this object wraps.
|
||||
/// </summary>
|
||||
public ConstructorInfo Constructor => Selection;
|
||||
|
||||
|
||||
#region Overrides
|
||||
|
||||
public override IEnumerable<ConstructorInfo> DeclaredMembers(Type type)
|
||||
{
|
||||
#if NETCOREAPP1_0 || NETSTANDARD1_0
|
||||
return type.GetTypeInfo().DeclaredConstructors
|
||||
.Where(c => c.IsStatic == false && c.IsPublic);
|
||||
#else
|
||||
return type.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
|
@ -1,13 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Unity.Builder;
|
||||
using Unity.Injection;
|
||||
using Unity.Interception.ContainerIntegration.Selection;
|
||||
using Unity.Interception.InterceptionBehaviors;
|
||||
using Unity.Interception.Interceptors;
|
||||
using Unity.Policy;
|
||||
using Unity.Strategies;
|
||||
|
||||
namespace Unity.Interception.ContainerIntegration.ObjectBuilder
|
||||
|
@ -143,91 +139,6 @@ namespace Unity.Interception.ContainerIntegration.ObjectBuilder
|
|||
public IEnumerable<IInterceptionBehavior> Behaviors { get; set; }
|
||||
}
|
||||
|
||||
private class DerivedTypeConstructorSelectorPolicy : ISelect<ConstructorInfo>
|
||||
{
|
||||
internal readonly Type InterceptingType;
|
||||
internal readonly ISelect<ConstructorInfo> OriginalConstructorSelectorPolicy;
|
||||
|
||||
internal DerivedTypeConstructorSelectorPolicy(
|
||||
Type interceptingType,
|
||||
ISelect<ConstructorInfo> originalConstructorSelectorPolicy)
|
||||
{
|
||||
InterceptingType = interceptingType;
|
||||
OriginalConstructorSelectorPolicy = originalConstructorSelectorPolicy;
|
||||
}
|
||||
|
||||
|
||||
public IEnumerable<object> Select(Type type, IPolicySet registration)
|
||||
{
|
||||
object originalConstructor =
|
||||
OriginalConstructorSelectorPolicy.Select(type, registration)
|
||||
.First();
|
||||
switch (originalConstructor)
|
||||
{
|
||||
case ConstructorInfo info:
|
||||
return new []{ FromConstructorInfo(info, InterceptingType) };
|
||||
|
||||
case SelectedConstructor selectedConstructor:
|
||||
return new []{ FromSelectedConstructor(selectedConstructor, InterceptingType) };
|
||||
|
||||
case MethodBase<ConstructorInfo> methodBaseMember:
|
||||
return new []{ FromMethodBaseMember(methodBaseMember, InterceptingType) };
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Unknown type");
|
||||
}
|
||||
|
||||
|
||||
private static SelectedConstructor FromMethodBaseMember(MethodBase<ConstructorInfo> methodBaseMember, Type type)
|
||||
{
|
||||
var cInfo = methodBaseMember.MemberInfo(type);
|
||||
var args = methodBaseMember.Data;
|
||||
|
||||
var newConstructorInfo = type.GetConstructor(cInfo.GetParameters().Select(pi => pi.ParameterType).ToArray());
|
||||
|
||||
return new SelectedConstructor(newConstructorInfo, args);
|
||||
}
|
||||
|
||||
private static SelectedConstructor FromSelectedConstructor(SelectedConstructor selectedConstructor, Type interceptingType)
|
||||
{
|
||||
var originalParams = selectedConstructor.Constructor.GetParameters();
|
||||
|
||||
var newConstructorInfo =
|
||||
interceptingType.GetConstructor(originalParams.Select(pi => pi.ParameterType).ToArray());
|
||||
|
||||
var newConstructor = new SelectedConstructor(newConstructorInfo, originalParams);
|
||||
|
||||
return newConstructor;
|
||||
}
|
||||
|
||||
private static SelectedConstructor FromConstructorInfo(ConstructorInfo info, Type interceptingType)
|
||||
{
|
||||
var originalParams = info.GetParameters();
|
||||
|
||||
var newConstructorInfo = interceptingType.GetConstructor(originalParams.Select(pi => pi.ParameterType).ToArray());
|
||||
|
||||
return new SelectedConstructor(newConstructorInfo);
|
||||
}
|
||||
|
||||
public static void SetPolicyForInterceptingType(ref BuilderContext context, Type interceptingType)
|
||||
{
|
||||
var currentSelectorPolicy =
|
||||
GetPolicy<ISelect<ConstructorInfo>>(ref context);
|
||||
|
||||
if (!(currentSelectorPolicy is DerivedTypeConstructorSelectorPolicy currentDerivedTypeSelectorPolicy))
|
||||
{
|
||||
context.Registration.Set(typeof(ISelect<ConstructorInfo>),
|
||||
new DerivedTypeConstructorSelectorPolicy(interceptingType, currentSelectorPolicy));
|
||||
}
|
||||
else if (currentDerivedTypeSelectorPolicy.InterceptingType != interceptingType)
|
||||
{
|
||||
context.Registration.Set(typeof(ISelect<ConstructorInfo>),
|
||||
new DerivedTypeConstructorSelectorPolicy(interceptingType,
|
||||
currentDerivedTypeSelectorPolicy.OriginalConstructorSelectorPolicy));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче