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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:55:51+00:00 2026-05-25T13:55:51+00:00

Consider the following situation: [Export] class A { } class B { [Import] private

  • 0

Consider the following situation:

[Export]
class A { }

class B 
{
  [Import]
  private A a;
}

// Instantiates class B.
class C 
{   
  public C(Type type){ /*Instantiate Class B here.*/}

  public void PerfomOperationUsingClassB() { }
}

class D
{
  void Initialize()
  {
    var catalog = new AssemblyCatalog(Assembly.GetAssembly(typeof(A)));
    var container = new CompositionContainer(catalog);

    // Is there any way to compose A with B?

    C c = new C(typeof(B));
    c.PerfomOperationUsingClassB();
  }
}

Problem: I have an access to class “A” and “B” but class “C” is located in the assembly that I can’t modify (thus can’t modify the class “C”). Is there any way to compose “A” and “B”?

  • 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-25T13:55:52+00:00Added an answer on May 25, 2026 at 1:55 pm

    Unfortunately, without access to whatever the constructor of C is doing, you pretty much out of luck. The constructor of C seems to want to initialise an instance of B using the type itself… does it provide any mechanism of intercepting the initialisation of B within C?

    If C were to accept an instance of B instead, you could easily just pass in a composed instance of B into the constructor. If you wanted to export the closed type, C, you could do something like:

    public class MefAdapter<T, TExport>
    {
      private readonly Func<T, TExport> _factory = CreateFactory();
      private readonly T _arg;
    
      [ImportingConstructor]
      public MefAdapter(T arg)
      {
        _arg = arg;
      }
    
      [Export]
      public TExport Export
      {
        get { return _factory(_arg); }
      }
    
      internal static Func<T, TExport> CreateFactory()
      {
        var tArg = typeof(T);
        var tExport = typeof(TExport);
    
        var arg = Expression.Parameter(tArg, "arg");
        var ctor = tExport.GetConstructor(new[] { tArg });
        var ctorExp = Expression.New(ctor, arg);
    
        return Expression.Lamda<Func<T, TExport>>(ctorExp, arg).Compile();
      }
    }
    

    (This is based on Mark Seemann’s Resolving closed types article).

    With that, should C actually look like:

    public class C
    {
      public C(B b)
      {
    
      }
    }
    

    You could have that closed type satisfied by an automatic creation of B:

    var typeCatalog = new TypeCatalog(typeof(MefAdapter<C>));
    var catalog = new AggregateCatalog(new DirectoryCatalog("."), typeCatalog);
    
    var container = new CompositionContainer(catalog);
    
    var c = container.ComposeExportedValue<C>();
    // This instance of C should have a composed instance of B injected.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following situation class URISplit { var $REQ_URI; //some more variables function __construct($uri)
Consider the following situation: Class** array = new Class*[8]; array[1] = new Class(1,2); Is
Consider the following situation. -(void) foo { Object * obj = [[Object alloc] init];
Please pardon me for a n00bish question. Please consider the following code: public class
Consider the following classes: class Coord { public: double _x, _y; Coord(double x, double
Consider the following method signatures: public fooMethod (Foo[] foos) { /*...*/ } and public
Consider the following code: void Handler(object o, EventArgs e) { // I swear o

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.