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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:14:52+00:00 2026-05-18T08:14:52+00:00

I’m looking to use MEF for a plugin system for an application I’m building.

  • 0

I’m looking to use MEF for a plugin system for an application I’m building. Each component I want to have an identifier on (a GUID) which I want to be able to look up against. But this ID is also something that is useful when working with the exported part.

Is there a way that I can have a Metadata attribute which contains the ID as well as a property (or method) on the exported part, short of having developers fill it out twice or use reflection to find it from the attribute?

  • 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-18T08:14:52+00:00Added an answer on May 18, 2026 at 8:14 am

    It’s likely to be a mixture of a MEF metadata attribute, and an abstract base class. I would define my plugin contract as something like:

    public interface IPluginMetadata
    {
      Guid PluginId { get; }
    }
    
    public interface IPlugin : IPluginMetadata
    {
      void Initialise();
    }
    

    I’ve enforced that the IPlugin interface also inherits our metadata contract IPluginMetadata. Next, we can create a custom export attribute:

    [AttributeUsage(AttributeTargets.Class, Inherit = true), MetadataAttribute]
    public class ExportPluginAttribute : ExportAttribute, IPluginMetadata
    {
      public ExportPluginAttribute(string pluginId) : base(typeof(IPlugin))
      {
        if (string.IsNullOrEmpty(pluginId))
          throw new ArgumentException("'pluginId' is required.", "pluginId");
    
        PluginId = new Guid(pluginId);
      }
    
      public Guid PluginId { get; private set; }
    }
    

    You don’t need to decorate the export attribute with the metadata contract IPluginMetadata, as MEF will project the properties anyway, but I prefer to do so, so if I do introduce changes to my metadata contract, then my export attribute should be updated too. No harm, no foul.

    Once we’ve done this, we can define an abstract base class from which to implement our plugin contract:

    public abstract class PluginBase : IPlugin
    {
      protected PluginBase()
      {
        var attr = GetType()
          .GetCustomAttributes(typeof(ExportPluginAttribute), true)
          .Cast<ExportPluginAttribute>()
          .SingleOrDefault();
    
        PluginId = (attr == null) ? Guid.Empty : attr.PluginId;
      }
    
      public virtual Guid PluginId { get; private set; }
    
      public abstract void Initialise();
    }
    

    We can then grab the custom attribute through the abstract class’s constructor, and apply the property accordingly. That we can do:

    public IPlugin GetPlugin(Guid id)
    {
      var plugin = container
        .GetExports<IPlugin, IPluginMetadata>()
        .Where(p => p.Metadata.PluginId == id)
        .Select(p => p.Value)
        .FirstOrDefault();
    
      return plugin;
    }
    

    And also:

    [ExportPlugin("BE112EA1-1AA1-4B92-934A-9EA8B90D622C")]
    public class MyPlugin : PluginBase
    {
      public override Initialise()
      {
        Console.WriteLine(PluginId);
      }
    }
    

    We can see that out PluginId is exposed both through exported metadata, as well as a property of our plugin.

    That code is all untested, but I hope it pushes you in the right direction.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build

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.