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

  • SEARCH
  • Home
  • 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 8791467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:50:49+00:00 2026-06-13T22:50:49+00:00

I am trying to understand MEF and did a few samples on that. But,

  • 0

I am trying to understand MEF and did a few samples on that. But, when I try to execute this particular code below, I am with an exception.

ConsoleApplication2.program

using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace ConsoleApplication2
{
    class Program 
    {
     [Import(typeof(Contracts.IInput))]
     public Contracts.IInput myinterface { get; set; }

    public void Method()
    {
        var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        var container = new CompositionContainer(catalog);
        container.ComposeParts(container );
        Console.WriteLine(myinterface.IsValid());
    }

    public static void Main(string[] args)
    {
        var obj = new Program();
        obj.Method();
        Console.ReadLine();
    }

   }
}

I have a separate project for defining an interface.

namespace Contracts
{
   public interface IInput
    {
      //All classes that inherit IInput must implement the IsValid Property.
      string IsValid();
    }
}

And in another separate project, I use a export class.

 using System.ComponentModel.Composition;
 using Contracts;

 namespace Plugin
 {
    [Export(typeof(Contracts.IInput))]
    public class Plugin : IInput
    {
        public string IsValid()
        {
             return "1";
        }
    }
 }

Now, all these produce (two library files and an executable) at the same “bin\Debug” folder.But on executing when reaching the code, “composeParts(this)” an exception occurs like below,

Object reference not set to an instance of an object.

When I try to change “Composeparts(container)” there is another exception like below,”

**> The composition produced a single composition error. The root cause is

provided below. Review the CompositionException.Errors property for
more detailed information.

1) Cannot populate the collection
‘ConsoleApplication2.Program.myinterface’ because it does not
implement ICollection or is read-only. If the collection is not
IEnumerable or T[] it must implement ICollection and be either
pre-initialized or be writable with a default constructor.

Resulting in: Cannot activate part ‘ConsoleApplication2.Program’
Element: ConsoleApplication2.Program –> ConsoleApplication2.Program **

  1. Can you please show me where I am going wrong?
  2. I thought both composeparts(this) and composeparts(container) were both same since I use Assembly.GetExecutingAssembly(). If so, then why did two different exceptions get thrown?

I used importmany like

class Program 
 {
    [ImportMany(typeof(Contracts.IInput))]
    public IEnumerable<Contracts.IInput> myinterface { get; set; }

    public void Method()
    {
        var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        var container = new CompositionContainer(catalog);

        try
        {
            container.ComposeParts(container);
            foreach (var i in myinterface)
                Console.WriteLine(i.IsValid());
        }
        catch (CompositionException compositionException)
        {

            Console.WriteLine(compositionException.ToString());
        }



    }

But in this case, still myinterface is null and this throws a null reference exception.

  • 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-13T22:50:49+00:00Added an answer on June 13, 2026 at 10:50 pm

    There are 2 problems:

    First:
    Use Import not ImportMany.

    OR
    Since ImportMany expects a collection so you should use something like this:

    public IEnumerable<Contracts.IInput> myinterface { get; set; }

    Second:

    Just as you cannot use properties that have the Import attribute on them in the Constructor (Thats what an importing constructor is for), You can’t access them in the program’s Main function.

    This, for instance, will work:

    var catalog = new AggregateCatalog();
    //Add all the parts found in all assemblies in
    //the same directory as the executing program
    
    catalog.Catalogs.Add(
                    new DirectoryCatalog(
                        Path.GetDirectoryName(
                        Assembly.GetExecutingAssembly().Location
                        )
                    )
                );
    
    var container = new CompositionContainer(catalog);
    container.ComposeParts(container);
    
    // Use the container to get a value for myinterface
    myinterface = container.GetExportedValue<Contracts.IInput>();
    Console.WriteLine(myinterface.IsValid());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
Trying to understand the math of this code snippet. A token is provided which
I am trying understand how multi queries work in mysqli. But I confess that
Trying to understand this binding process of the WPF. See the code at the
Trying to understand how the code below always brings up a number between 0-6
just trying to understand this code. $db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
Trying to understand this MSDN sample but I'm confused about these lines: IAsyncResult result
Trying to understand PNG format. Consider this PNG Image: The Image is taken from
Trying to understand the options for will_paginate's paginate method: :page — REQUIRED, but defaults
Im trying to understand how class generics work and this bit just doesnt make

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.