Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ private IEnumerable<CompositionDependency> GetPartActivatorDependencies(Dependen
}
}

if (_constructor == null && _partType.IsGenericType)
{
_constructor = GetConstructorInfoFromGenericType(_partType);
}

if (_constructor == null)
_constructor = _partType.DeclaredConstructors
.FirstOrDefault(ci => ci.IsPublic && !(ci.IsStatic || ci.GetParameters().Any()));
Expand Down Expand Up @@ -130,6 +135,34 @@ private IEnumerable<CompositionDependency> GetPartActivatorDependencies(Dependen
}
}

private ConstructorInfo GetConstructorInfoFromGenericType(TypeInfo type)
{
var genericPartType = type.GetGenericTypeDefinition();
var genericPartTypeInfo = genericPartType.GetTypeInfo();
ConstructorInfo constructor = null;

int index = -1;
foreach (var c in genericPartTypeInfo.DeclaredConstructors)
{
++index;

if (!c.IsPublic || c.IsStatic) continue;

if (_attributeContext.GetDeclaredAttribute<ImportingConstructorAttribute>(genericPartType, c) != null)
{
if (constructor != null)
{
string message = SR.Format(SR.DiscoveredPart_MultipleImportingConstructorsFound, type);
throw new CompositionFailedException(message);
}

constructor = type.DeclaredConstructors.ElementAt(index);
}
}

return constructor;
}

public CompositeActivator GetActivator(DependencyAccessor definitionAccessor, IEnumerable<CompositionDependency> dependencies)
{
if (_partActivator != null) return _partActivator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Composition.Convention;
using System.Composition.Hosting.Core;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using Xunit;

Expand Down Expand Up @@ -591,5 +592,45 @@ public void DebuggerAttributes_GetViaReflection_Success(ContainerConfiguration c
Assert.NotNull(property.GetValue(debuggerAttributeInfo.Instance));
}
}

[Fact]
public void CreateContiner_GenericExportWithDependencyConstructorHasConvention_Success()
{
var conventions = new ConventionBuilder();

conventions
.ForType<Dependency>()
.Export<Dependency>();

conventions
.ForType(typeof(MoreOpenWithDependency<>))
.ExportInterfaces(
(i) => i.GetGenericTypeDefinition() == typeof(IOpen<>),
(type, builder) => builder.AsContractType(typeof(IOpen<>)))
.SelectConstructor(ctors => ctors.ElementAt(0));

var configuration = new ContainerConfiguration()
.WithParts(new[] { typeof(IOpen<>), typeof(MoreOpenWithDependency<>), typeof(Dependency) }, conventions);

using (var container = configuration.CreateContainer())
{
var service = container.GetExport(typeof(IOpen<object>)) as MoreOpenWithDependency<object>;
Assert.NotNull(service);
Assert.NotNull(service.Dependency);
}
}
public interface IOpen<T>
{
}
public class MoreOpenWithDependency<T> : IOpen<T>
{
public Dependency Dependency { get; set; }
public MoreOpenWithDependency(Dependency dep)
{
Dependency = dep;
}
}

public class Dependency { }
}
}