Compiling open shared types with closed types in MEF 2

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" )

+4
2

conventions,

catalog = new AssemblyCatalog(typeof(FooUser).Assembly, conventions);

catalog = new AssemblyCatalog(typeof(FooUser).Assembly);

conventions - , FooUser Something, Something. Something .

+1

MEF . :

[Export]
public class Foo<T> where T : Bar { }
[Export]
public class FooUnconstrained<T> { }
[Export]
public class Bar { }

, RegistrationBuilder:

using (var catalogue = new ApplicationCatalog(new RegistrationBuilder()))
using (var container = new CompositionContainer(catalogue))
{
    Console.WriteLine(container.GetExport<FooUnconstrained<Bar>>().Value);
}

RegistrationBuilder:

using (var catalogue = new ApplicationCatalog())
using (var container = new CompositionContainer(catalogue))
{
    Console.WriteLine(container.GetExport<Foo<Bar>>().Value);
}

RegistrationBuilder:

using (var catalogue = new ApplicationCatalog(new RegistrationBuilder()))
using (var container = new CompositionContainer(catalogue))
{
    // Next line throws ImportCardinalityMismatchException:
    Console.WriteLine(container.GetExport<Foo<Bar>>().Value);
}

.net. releaseKey = 528040 ( .net-4.8).

, , . , , , . , .

0

All Articles