MEF GetExports<T, TMetaDataView> returning nothing with AllowMultiple = True
Posted
by
sohum
on Stack Overflow
See other posts from Stack Overflow
or by sohum
Published on 2012-06-11T22:38:22Z
Indexed on
2012/06/11
22:39 UTC
Read the original article
Hit count: 236
I don't understand MEF very well, so hopefully this is a simple fix of how I think it works.
I'm trying to use MEF to get some information about a class and how it should be used. I'm using the Metadata options to try to achieve this. My interfaces and attribute looks like this:
public interface IMyInterface
{
}
public interface IMyInterfaceInfo
{
Type SomeProperty1 { get; }
double SomeProperty2 { get; }
string SomeProperty3 { get; }
}
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class ExportMyInterfaceAttribute : ExportAttribute, IMyInterfaceInfo
{
public ExportMyInterfaceAttribute(Type someProperty1, double someProperty2, string someProperty3)
: base(typeof(IMyInterface))
{
SomeProperty1 = someProperty1;
SomeProperty2 = someProperty2;
SomeProperty3 = someProperty3;
}
public Type SomeProperty1 { get; set; }
public double SomeProperty2 { get; set; }
public string SomeProperty3 { get; set; }
}
The class that is decorated with the attribute looks like this:
[ExportMyInterface(typeof(string), 0.1, "whoo data!")]
[ExportMyInterface(typeof(int), 0.4, "asdfasdf!!")]
public class DecoratedClass : IMyInterface
{
}
The method that is trying to use the import looks like this:
private void SomeFunction()
{
// CompositionContainer is an instance of CompositionContainer
var myExports = CompositionContainer.GetExports<IMyInterface, IMyInterfaceInfo>();
}
In my case myExports
is always empty. In my CompositionContainer, I have a Part in my catalog that has two ExportDefinitions
, both with the following ContractName
: "MyNamespace.IMyInterface". The Metadata
is also loaded correctly per my exports.
If I remove the AllowMultiple
setter and only include one exported attribute, the myExports
variable now has the single export with its loaded metadata.
What am I doing wrong?
© Stack Overflow or respective owner