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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:47:09+00:00 2026-05-27T16:47:09+00:00

I got a ViewModel which I export with MEF. I’d like this ViewModel to

  • 0

I got a ViewModel which I export with MEF. I’d like this ViewModel to be initialized differently each time it’s being imported, according to an enum/specific object parameter that will be provided to it.

I’ve been reading a little on the subject and I found that maybe this –

http://msdn.microsoft.com/en-us/library/ee155691.aspx#metadata_and_metadata_views

would be able to fit my needs, but I’m not sure that this would be the best way to do it.

Another method I’ve been thinking about is importing the class normally, and then once I’ve an instance, to call a special initialization method that would receive my parameter. However this doesn’t seem like a classic MEF implementation, and maybe losses some of its “magic”.

I’m hoping someone would be able to point out for me what would be the recommended method to achieve this.

Thanks!

  • 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-27T16:47:10+00:00Added an answer on May 27, 2026 at 4:47 pm

    A workaround is exporting a factory that creates instances of your type. While this means you cannot directly import thos instances, it does have the benefit that the logic to create them is the responsability of the factory so users of the class do not have to know about it:

    public class ServiceWithParameter
    {
      public ServiceWithParameter( int a )
      {
        this.a = a;
      }
    
      private readonly int a;
    }
    
    [Export]
    public class ServiceWithParameterFactory
    {
      public ServiceWithParameterFactory()
      {
        instance = 0;
      }
    
      public ServiceWithParameter Instance()
      {
        return new ServiceWithParameter( instance++ );
      }
    
      private int instance;
    }
    
      //now everywhere you need ServiceWithParameter:
    [Import]
    ServiceWithParameterFactory serviceFactory;
    
    var instanceA = serviceFactory.Instance(); //instanceA.a = 0
    var instanceB = serviceFactory.Instance(); //instanceB.a = 1
    

    A more extensible way is telling the container you have a factory and an example is presented here: http://pwlodek.blogspot.com/2010/10/mef-object-factories-using-export.html

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

Sidebar

Related Questions

I've got a WPF application which calls MessageBox.Show() way back in the ViewModel (to
I have got a collection of viewModels(InputViewModel) in an other viewModel(ScenarioManager). each InputviewModel has
I've got an app which is using a WCF service. Now I'd like to
I've got a series of views, each are typed to have their own ViewModel
I've got a parent UserControl which has a property which I would like to
I have a viewmodel which implement INotifyPropertyChanged . On this viewModel is a property
I've got a ViewModel for adding a user with properties: Email, Password, ConfirmPassword with
Ok, i got a viewmodel as follows: public class PageViewModel { public Item Item
In my View I got a ListView bound to a CollectionView in my ViewModel,
I have a main page which uses a ViewModel I have created: public class

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.