Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8088887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:13:28+00:00 2026-06-05T19:13:28+00:00

I don’t understand MEF very well, so hopefully this is a simple fix of

  • 0

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?

EDIT: If I use weakly typed Metadata, my export is suddenly satisfied:

var myExports = CompositionContainer.GetExports<IMyInterface, IDictionary<string, object>>();

Any ideas why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T19:13:29+00:00Added an answer on June 5, 2026 at 7:13 pm

    It is known that MEF has some problems when dealing with AllowMultiple = true. For a complete explanation you could for instance look here, anyway it derives from the fact that the metadata are saved in a Dictionary where the values are arrays when AllowMultiple is true, and such a thing can’t be mapped on your IMyInterfaceInfo.

    This is the workaround I use. First of all your attribute should derive from Attribute, not from ExportAttribute:

    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
    public class ExportMyInterfaceAttribute : Attribute, IMyInterfaceInfo
    {
        public ExportMyInterfaceAttribute(Type someProperty1, double someProperty2, string someProperty3)
    
        {
            SomeProperty1 = someProperty1;
            SomeProperty2 = someProperty2;
            SomeProperty3 = someProperty3;
        }
    
        public Type SomeProperty1 { get; set; }
        public double SomeProperty2 { get; set; }
        public string SomeProperty3 { get; set; }
    }
    

    This means that the exported class should have 3 attributes, a standard export and your custom attributes:

    [Export(typeof(IMyInterface))]
    [ExportMyInterface(typeof(string), 0.1, "whoo data!")]
    [ExportMyInterface(typeof(int), 0.4, "asdfasdf!!")]
    public class DecoratedClass : IMyInterface
    

    Then you have to define a view for the metadata which will be imported. This has to have a constructor that takes the IDictionary as parameter. Something like this:

    public class MyInterfaceInfoView
    {
        public IMyInterfaceInfo[] Infos { get; set; }
    
        public MyInterfaceInfoView(IDictionary<string, object> aDict)
        {
            Type[] p1 = aDict["SomeProperty1"] as Type[];
            double[] p2 = aDict["SomeProperty2"] as double[];
            string[] p3 = aDict["SomeProperty3"] as string[];
    
            Infos = new ExportMyInterfaceAttribute[p1.Length];
            for (int i = 0; i < Infos.Length; i++)
                Infos[i] = new ExportMyInterfaceAttribute(p1[i], p2[i], p3[i]);
        }
    }
    

    Now you should be able to successfully call

    var myExports = CompositionContainer.GetExports<IMyInterface, MyInterfaceInfoView>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't think that I'm mad, I understand how php works! That being said. I
Don't understand this simple code: def main(): print (This program illustrates a chaotic function)
Don't overthink this - there's a very commonly used term and I ... have
Don't think my virtualhost is working correctly. This is what I have inside of
Don't know how to explain it better but i'm trying to get a response
Don't understand, if Data.Map is and [] is. I found this out while wondering
Don't know if I'm over-thinking this or not.. but I'm trying to be able
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Don't ask me why but i can't use this method because I need to
Don't think this is a repost, difficult to search for the word between because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.