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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:08:28+00:00 2026-06-06T19:08:28+00:00

First, the simple question. Is it possible to receive an event when MEF (System.ComponentModel.Composition)

  • 0

First, the simple question.

Is it possible to receive an event when MEF (System.ComponentModel.Composition) creates an instance of a part?
When this occurs I want to reflect over the created object and wire up various attributes. In Spring.Net this is possible with the IObjectPostProcessor interface.

The background is that I trying to implement Publisher/Subscriber pattern in MEF. Basically the subscriber class does this:

class MyContoller
{
   [Command("Print")]
   public void Print() { ... }

   [Command("PrintPreview")]
   public void PrintPreview() { ... }
}

And I want to detect when MyController is instantiated and wire up any methods that have the CommandAttribute.

A publisher, such as a menu item, would do Command.Get("Print").Fire() to publish the aforementioned event.

Second Question

Maybe there is an alternative pattern in MEF that I am missing!!!

I’ve seen some postings about MEF, Prism and the Event Aggregate, but it appears fairly complex.

FYI

Just for reference, here’s the original for Spring.Net implementation:

class CommandAttributeProcessor : IObjectPostProcessor
{
  static ILog log = LogManager.GetLogger(typeof(CommandAttributeProcessor));

  public object PostProcessAfterInitialization(object instance, string objectName)
  {
     foreach (MethodInfo methodInfo in instance.GetType().GetMethods())
     {
        foreach (CommandAttribute attr in methodInfo.GetCustomAttributes(typeof(CommandAttribute), true))
        {
           if (log.IsDebugEnabled)
              log.Debug(String.Format("Binding method '{0}.{1}' to command '{2}'.", instance.GetType().Name, methodInfo.Name, attr.CommandName));

           Command command = Command.Get(attr.CommandName);
           command.Execute += (EventHandler) Delegate.CreateDelegate(typeof(EventHandler), instance, methodInfo);
        }
     }
     return instance;
  }

  public object PostProcessBeforeInitialization(object instance, string name)
  {
     return instance;
  }

}

  • 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-06T19:08:30+00:00Added an answer on June 6, 2026 at 7:08 pm

    You can use InterceptingCatalog from MEF Contrib (MEF Contrib on codeplex or you can install it by nuGet) and implement IExportedValueInterceptor interface to wire up methods that have CommandAttribute:

    //using System.ComponentModel.Composition;
    //using System.ComponentModel.Composition.Hosting;
    //using MefContrib.Hosting.Interception;
    //using MefContrib.Hosting.Interception.Configuration;
    
    public class CommandAttributeProcessor : IExportedValueInterceptor
    {
        public object Intercept(object value)
        {
            foreach (MethodInfo methodInfo in value.GetType().GetMethods())
            {
                foreach (CommandAttribute attr in methodInfo.GetCustomAttributes(typeof(CommandAttribute), true))
                {
                    // do something with command attribute
                }
            }
    
            return value;
        }
    }
    

    and at creating MEF catalog, you need to add interception configuration with your interceptor (CommandAttributeProcessor) and wrap your catalog in InterceptingCatalog like this:

    InterceptionConfiguration interceptionConfiguration = new InterceptionConfiguration();
    interceptionConfiguration.AddInterceptor(new CommandAttributeProcessor());
    InterceptingCatalog interceptingCatalog = new InterceptingCatalog(assemblyCatalog, interceptionConfiguration);
    CompositionContainer container = new CompositionContainer(interceptingCatalog);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is just a simple question. Either way works. I prefer my first example,
this is my first question on here. The question is simple - # this
This is my first question here and I hope it is simple enough to
It's possible that this question either has a simple answer that's a standard practice,
First I want to make clear this is not a simple LIMIT x,y question.
First off, I'm sorry for asking such a simple question in such a sophisticated
Possible Duplicate: Python conditional assignment operator Apologies for such a simple question, but googling
This is probably a super simple question, but I'm struggling to come up with
This is my first question her so please be gentle: I have followig animation
This question might appear very simple, but i haven't found an answer yet, so

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.