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 7171345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:23:47+00:00 2026-05-28T15:23:47+00:00

I have a Prim class that implements an IPrimitiveDecomposer interface for MEF and inherits

  • 0

I have a Prim class that implements an IPrimitiveDecomposer interface for MEF and inherits Node base class.

public class Node
{
    public Node()
    {
    }
}

public interface IPrimitiveDecomposer
{
    bool Match(Node node);
}

[Export(typeof(IPrimitiveDecomposer))]
public class Prim : Node, IPrimitiveDecomposer
{       
    public bool Match(Node node) {return true;}
}

However, when I inherit a class that doesn’t have a parameterless constructor, MEF’s ComposeParts() method cannot import the Prim object. I added the attribute of ImportingConstructor following this page in MSDN, as I got compilation error without the attribute.

[Export(typeof(IPrimitiveDecomposer))]
public class Prim : Node, IPrimitiveDecomposer
{
    [ImportingConstructor] 
    public Prim(int val) : base (val)
    {}

    public bool Match(Node node) {return true;}
}

The code that doesn’t work is as follows. When you provide the parameterless constructor for the Node class, it works.

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

public class Node
{
    public Node(int val)
    {
    }
}

public interface IPrimitiveDecomposer
{
    bool Match(Node node);
}

[Export(typeof(IPrimitiveDecomposer))]
public class Prim : Node, IPrimitiveDecomposer
{
    [ImportingConstructor] 
    public Prim(int val) : base (val)
    {}

    public bool Match(Node node) {return true;}
}

public class Test
{
    [ImportMany(typeof(IPrimitiveDecomposer), AllowRecomposition = true)]
    private IEnumerable<IPrimitiveDecomposer> PrimitiveDecomposers { get; set; }

    void mef()
    {
        // MEF
        var catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

        var container = new CompositionContainer(catalog);
        container.ComposeParts(this);
    }

    static void Main()
    {
        var mef = new Test();
        mef.mef();
        var res = mef.PrimitiveDecomposers;

        foreach(var it in res)
        {
            Console.WriteLine(it);
        }
    }
}
  • 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-05-28T15:23:48+00:00Added an answer on May 28, 2026 at 3:23 pm

    The ImportingConstructor attribute only works when the parameters to the constructor are MEF exported objects. The Prim(int val) constructors fails composition because MEF doesn’t know what value to provide for the constructor.

    This particular scenario looks like it’s much more suited for a MEF factory pattern.

    interface IPrimitiveDecomposerFactory {
      IPrimitiveDecomposer Create(int value);
    }
    
    [Export(typeof(IPrimitiveDecomposerFactory))]
    sealed class PrimitiveDecomposerFactor : IPrimitiveDecomposerFactory {
      public IPrimitiveDecomposer Create(int value) {
        return new Prim(value);
      }
    }
    

    Now code can import the IPrimitiveDecomposerFactory and use it to create IPrimitiveDecomposer instance based on specific int values

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a code that solves MST using Prim method. I read that
Have a web service that implements REST (sort of) , Client request is made
Can anyone help? I have a created a relationship between my Reservation(prim key) and
have written this little class, which generates a UUID every time an object of
I have this question for my programming class which I have been struggling to
I have a class inside the main class named Prims and a priority queue
I have a structure like below that needs to be saved and loaded from
I have a query that I have simplified and written below. How do I
I intuitively feel that if one is using Prim's algorithm to find a graph's
I have this code on jsFiddle that is implemented and works just fine on

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.