Resolving issue with Optional Generic Parameter

Fixed #293
This commit is contained in:
Eugene Sadovoi 2020-11-23 13:18:45 -08:00
Родитель e65799e273
Коммит 07df2796aa
6 изменённых файлов: 51 добавлений и 16 удалений

Просмотреть файл

@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<VersionBase>5.11.9</VersionBase>
<VersionBase>5.11.10</VersionBase>
<PackageReleaseNotes>This package is compatible with .NET Standard 1.0 and 2.0, .NET Core 1.0 and 2.0, .NET 4.0, 4.5, 4.6, 4.7</PackageReleaseNotes>
</PropertyGroup>

Просмотреть файл

@ -12,13 +12,15 @@ namespace Unity.Processors
protected virtual IEnumerable<Expression> ExpressionsFromSelection(Type type, IEnumerable<object> members)
{
HashSet<TMemberInfo> memberSet = new HashSet<TMemberInfo>();
foreach (var member in members)
{
switch (member)
{
// TMemberInfo
case TMemberInfo info:
if (!memberSet.Add(info)) continue;
object value = DependencyAttribute.Instance;
foreach (var node in AttributeFactories)
{
@ -34,8 +36,9 @@ namespace Unity.Processors
// Injection Member
case InjectionMember<TMemberInfo, TData> injectionMember:
yield return GetResolverExpression(injectionMember.MemberInfo(type),
injectionMember.Data);
var selection = injectionMember.MemberInfo(type);
if (!memberSet.Add(selection)) continue;
yield return GetResolverExpression(selection, injectionMember.Data);
break;
// Unknown

Просмотреть файл

@ -154,8 +154,8 @@ namespace Unity.Processors
public override ResolveDelegate<BuilderContext> GetResolver(Type type, IPolicySet registration, ResolveDelegate<BuilderContext> seed)
{
var selector = GetPolicy<ISelect<TMemberInfo>>(registration);
var members = selector.Select(type, registration);
var resolvers = ResolversFromSelection(type, members).ToArray();
var members = selector.Select(type, registration);
var resolvers = ResolversFromSelection(type, members).Distinct().ToArray();
return (ref BuilderContext c) =>
{
@ -172,14 +172,12 @@ namespace Unity.Processors
public virtual IEnumerable<object> Select(Type type, IPolicySet registration)
{
HashSet<object> memberSet = new HashSet<object>();
// Select Injected Members
if (null != ((InternalRegistration)registration).InjectionMembers)
{
foreach (var injectionMember in ((InternalRegistration)registration).InjectionMembers)
{
if (injectionMember is InjectionMember<TMemberInfo, TData> && memberSet.Add(injectionMember))
if (injectionMember is InjectionMember<TMemberInfo, TData>)
yield return injectionMember;
}
}
@ -193,12 +191,10 @@ namespace Unity.Processors
for (var i = 0; i < AttributeFactories.Length; i++)
{
#if NET40
if (!member.IsDefined(AttributeFactories[i].Type, true) ||
if (!member.IsDefined(AttributeFactories[i].Type, true)) continue;
#else
if (!member.IsDefined(AttributeFactories[i].Type) ||
if (!member.IsDefined(AttributeFactories[i].Type)) continue;
#endif
!memberSet.Add(member)) continue;
yield return member;
break;
}

Просмотреть файл

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Reflection;
using Unity.Builder;
using Unity.Injection;
using Unity.Policy;
using Unity.Resolution;
namespace Unity.Processors
@ -14,12 +13,15 @@ namespace Unity.Processors
protected virtual IEnumerable<ResolveDelegate<BuilderContext>> ResolversFromSelection(Type type, IEnumerable<object> members)
{
HashSet<TMemberInfo> memberSet = new HashSet<TMemberInfo>();
foreach (var member in members)
{
switch (member)
{
// MemberInfo
case TMemberInfo info:
if (!memberSet.Add(info)) continue;
object value = DependencyAttribute.Instance;
foreach (var node in AttributeFactories)
{
@ -34,8 +36,9 @@ namespace Unity.Processors
// Injection Member
case InjectionMember<TMemberInfo, TData> injectionMember:
yield return GetResolverDelegate(injectionMember.MemberInfo(type),
injectionMember.Data);
var selection = injectionMember.MemberInfo(type);
if (!memberSet.Add(selection)) continue;
yield return GetResolverDelegate(selection, injectionMember.Data);
break;
// Unknown

Просмотреть файл

@ -34,6 +34,7 @@ namespace Unity.Processors
#endregion
#region Expression
protected override Expression GetResolverExpression(MethodInfo info, object resolvers)

Просмотреть файл

@ -1,6 +1,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using Unity.Injection;
using Unity.Lifetime;
using Unity.Resolution;
@ -222,6 +223,37 @@ namespace Unity.Tests.v5.Issues
Assert.AreEqual(main2, main2.HelperClass.HostClass);
}
public class BaselineTestType<TDependency>
{
[Dependency] public TDependency Field;
public object Value { get => Field; protected set => throw new NotSupportedException(); }
public object Expected => default(TDependency);
}
[TestMethod]
public void unitycontainer_container_293()
{
var unnamed = 55;
var named = 22;
var name = "Name";
// Create root container and register classes in root
IUnityContainer container = new UnityContainer();
container.RegisterType(typeof(BaselineTestType<>), new InjectionField("Field", new OptionalGenericParameter("TDependency", name)));
var instance = container.Resolve<BaselineTestType<int>>();
Assert.IsNotNull(instance);
Assert.AreEqual(0, instance.Field);
container.RegisterInstance(unnamed)
.RegisterInstance(name, named);
instance = container.Resolve<BaselineTestType<int>>();
Assert.IsNotNull(instance);
Assert.AreEqual(named, instance.Field);
}
}
public class MainClass : IHostClass