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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:10:31+00:00 2026-05-23T09:10:31+00:00

I am trying to dynamically (using reflection) add types to a MEF catalog and

  • 0

I am trying to dynamically (using reflection) add types to a MEF catalog and also define the export contract at runtime. The issue is MEF will simply use the fully qualified name of the type as the contract, and I need to specify the export contract as a specific interface type.

Here is the code I use to get the types I need to add to the MEF catalog:

private void RegisterSupportedRepositories(Func<ObjectContext> contextProvider)
    {
        var context = contextProvider();
        var properties = context.GetType().GetProperties();
        var entitySets = properties.Where(property => property.PropertyType.Name == typeof(ObjectSet<>).Name);

        foreach (var entitySet in entitySets)
        {
            var entityType = entitySet.PropertyType.GetGenericArguments()[0];
            var repositoryType = typeof(EFRepository<>).MakeGenericType(entityType);
            ComponentServices.RegisterComponent(repositoryType);                
        }
    }

ComponentServices.RegisterComponent is defined below, it gets an AggregateCatalog (from where isn’t relevant) and then adds TypeCatalogs to the aggregate catalog:

public static void RegisterComponent(Type componentType)
    {
        var catalog = RetrieveCatalog();

        catalog.Catalogs.Add(new TypeCatalog(componentType));
    }

I need to be able to specify the contract of the added types as an interface, this would normally be done with Export attributes that look like this:

[Export(typeof(IRepository<Customer>))]

The question is how do I add this export contract dynamically, so that the export is equivalent to what’s shown above and not the default “EFRepository” contract that MEF infers from the type itself.

  • 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-23T09:10:31+00:00Added an answer on May 23, 2026 at 9:10 am

    If you can use the CodePlex preview version of MEF, I’d recommend using the new RegistrationBuilder to do this. Here’s a blog post describing it: MEF’s Convention Model

    If you can’t use the preview version, you can use the methods in ReflectionModelServices to create your own part definitions, and create a catalog implementation that uses them.

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

Sidebar

Related Questions

I'm trying to dynamically define functions that call through to another function that takes
Trying to make a make generic select control that I can dynamically add elements
I trying to export an HTML table named Table that is dynamically binded to
I am trying to dynamically generate content using JSP. I have a <c:forEach> loop
I'm trying to print some Japanese to a page dynamically using jQuery, and it
I'm trying to hide some divs that I have created dynamically using the .hide()
I am trying to dynamically filter out content using checkboxes and jquery. Being new
I'm trying to dynamically get a databases Table structure using only C# code as
I'm trying to use PHP reflection to dynamically load the class files of models
While I was trying to refresh page contents dynamically using Ajax/JQuery, I have learned

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.