Reputation: 2010
I'm using latest MEF 2 preview from Codeplex homepage, and it's supposed to add open-generics support. It does, but in this specific case, MEF fails to compose generic implementation of generic interface:
public interface IOuter
{
IInner Value { get; }
}
[Export(typeof(IOuter))]
public class MyOuter : IOuter
{
[ImportingConstructor]
public MyOuter(InnerGenericClass<string, int> value)
{
this.Value = value;
}
public IInner Value { get; private set; }
}
public interface IInner
{
void Say();
}
public interface IGenericInner<T, K> : IInner
{
// something else here
}
[Export(typeof(IGenericInner<,>))]
public class InnerGenericClass<T, K> : IGenericInner<T, K>
{
public void Say()
{
Console.WriteLine("{0}, {1}", typeof(T), typeof(K));
}
}
class Startup
{
public void CatalogSetup()
{
var catalog = new AggregateCatalog(
new AssemblyCatalog(Assembly.GetExecutingAssembly())
);
var container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
var batch = new CompositionBatch();
container.Compose(batch);
var outer = container.GetExportedValue<IOuter>();
outer.Value.Say();
}
}
Here's CompositionExpection:
The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) No exports were found that match the constraint:
ContractName ConsoleApplication1.InnerGenericClass(System.String,System.Int32)
RequiredTypeIdentity ConsoleApplication1.InnerGenericClass(System.String,System.Int32)
Resulting in: Cannot set import 'ConsoleApplication1.MyOuter.Value (ContractName="ConsoleApplication1.InnerGenericClass(System.String,System.Int32)")' on part 'ConsoleApplication1.MyOuter'.
Element: ConsoleApplication1.MyOuter.Value (ContractName="ConsoleApplication1.InnerGenericClass(System.String,System.Int32)") --> ConsoleApplication1.MyOuter --> AssemblyCatalog (Assembly="ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Resulting in: Cannot get export 'ConsoleApplication1.MyOuter (ContractName="ConsoleApplication1.IOuter")' from part 'ConsoleApplication1.MyOuter'.
Element: ConsoleApplication1.MyOuter (ContractName="ConsoleApplication1.IOuter") --> ConsoleApplication1.MyOuter --> AssemblyCatalog (Assembly="ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
Same exception is thrown even when I move importing of InnerGenericClass to attribute of MyOuter.Value:
[Export(typeof(IOuter))]
public class MyOuter : IOuter
{
[Import(typeof(InnerGenericClass<string, int>))]
public IInner Value { get; private set; }
}
What is weird, is that, it does work, when I change import type to IGenericInner:
[Export(typeof(IOuter))]
public class MyOuter : IOuter
{
[ImportingConstructor]
public MyOuter(IGenericInner<string, int> value)
{
this.Value = value;
}
public IInner Value { get; private set; }
}
What is even weirder, is that, it does not work, when importing via attribute.
Summary: I can't use generic interface to import object to Value property, because there could be more implementations of IGenericInner interface (and also I want to import a specific implementation, but that's not important).
I hope I won't have to bypass MEF altogether in this case.
Upvotes: 2
Views: 3060
Reputation: 2242
Cause your exporting the interface IGenericInner<> but want to import the specific class InnerGenericClass MEF does not find the correct part. You can export and import the specific class or create two exports of InnerGenericClass<> this way:
[Export(typeof(IGenericInner<,>))]
[Export(typeof(InnerGenericClass<,>))]
public class InnerGenericClass<T, K> : IGenericInner<T, K> {
public void Say() {
Console.WriteLine("{0}, {1}", typeof(T), typeof(K));
}
}
I just tested this in a VS .NET 4.5 project and it works. Btw. MEF 2 is already released in .NET 4.5 - If possible I recommend using System.ComponentModel.Composition.dll part of the framework not the preview version.
UPDATE: A third solution (which will work with the Preview Version 5) could be to use additionally a string contract name. I personally try to avoid this cause of the ugly syntax, but ok. Code will looks like this:
[Export(typeof(IOuter))]
public class MyOuter : IOuter {
[ImportingConstructor]
public MyOuter([Import("MySpecialInnerGenericClass", typeof(IGenericInner<,>))]InnerGenericClass<string, int> value) {
this.Value = value;
}
public IInner Value { get; private set; }
}
[Export("MySpecialInnerGenericClass", typeof(IGenericInner<,>))]
public class InnerGenericClass<T, K> : IGenericInner<T, K> {
public void Say() {
Console.WriteLine("{0}, {1}", typeof(T), typeof(K));
}
}
Upvotes: 5