I understand that, starting with MEF 2, MEF supports compiling open common types in private types. I am trying to compose a private type from types exported from two different assemblies added to the same build container, and I get an ImportCardinalityMismatchException. I use conventions for one of the assemblies because it is not under my control. For another, I used attributes.
I'm not quite sure how to talk about my question, since I find the terminology around generics quite confusing, but I want to create my new private type without explicitly implementing my own class that inherits from Foo and giving it my FooUser type. I do not know if this is due to the way I do it, or if this is due to the fact that the types are in different assemblies.
In one assembly, I have the following:
public class Foo<T> where T : Bar {}
public class Bar {}
In another build, I have the following:
[Export]
public class Bar2 : Bar {}
[Export]
public class Something
{
[ImportingConstructor]
public Something([Import(typeof(Foo<>))] Foo<Bar2> foo) {}
}
In my registration code, I did the following:
var conventions = new RegistrationBuilder();
conventions.ForType(typeof(Foo<>)).Export();
var aggregateCatalog = new AggregateCatalog();
var catalog = new AssemblyCatalog(typeof(Foo<>).Assembly, conventions);
aggregateCatalog.Catalogs.Add(catalog);
catalog = new AssemblyCatalog(typeof(Something).Assembly);
aggregateCatalog.Catalogs.Add(catalog);
catalog = new AssemblyCatalog(typeof(Bar2).Assembly);
aggregateCatalog.Catalogs.Add(catalog);
var container = new CompositionContainer(aggregateCatalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
batch.AddExportedValue(container);
container.Compose(batch);
Later I will try to export my value this way:
container.GetExportedValue<Something>();
: : " , : ContractName Foo (Bar2) RequiredTypeIdentity Foo (Bar2)" (System.ComponentModel.Composition.ImportCardinalityMismatchException)
A System.ComponentModel.Composition.ImportCardinalityMismatchException : " , : ContractName Foo (Bar2) RequiredTypeIdentity Foo (Bar2)"
, Foo {0}, Bar2 Something. System.ComponentModel.Composition.ImportCardinalityMismatchException.
, , IRepository, , - , . . , , , .
: , , , - , , . , .
. . CompositionException.Errors.
1) , : ContractName CompositionTestLibrary.Foo(CompositionTestLibrary2.Bar2) RequiredTypeIdentity CompositionTestLibrary.Foo(CompositionTestLibrary2.Bar2)
: . CompositionTest.Something..ctor(Parameter = "foo", ContractName = "CompositionTestLibrary.Foo(CompositionTestLibrary2.Bar2)" ) ' ' CompositionTest.Something '.
: CompositionTest.Something..ctor(Parameter = "foo", ContractName = "CompositionTestLibrary.Foo(CompositionTestLibrary2.Bar2)" ) → CompositionTest.Something → AssemblyCatalog (Assembly = "CompositionTest, Version = 1.0.0.0, = , PublicKeyToken = null" )
: 'CompositionTest.Something(ContractName = "CompositionTest.Something" )' 'CompositionTest.Something'.
: CompositionTest.Something(ContractName = "CompositionTest.Something" ) → CompositionTest.Something → AssemblyCatalog (Assembly = "CompositionTest, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null" )