How do I instantiate using Parameterized constructor

Sep 16, 2009 at 12:56 AM
Edited Sep 16, 2009 at 1:01 AM

I want to create a simple plugin model where I want a user to choose only 1 plugin among several available plugins located in a directory.  This simple task has proven very difficult.  So far, I've been able to locate the plugin and instantiate it, but I can't seem to instantiate it with a parameterized constructor.  I am using MEF Preview 7.  Here's my code so far:

 

    /// <summary>
    /// Selects a plugin based on the name
    /// </summary>
    /// <param name="name">Name of plugin stored in metadata</param>
    public void FindPluginByName(string name)
    {
        Lazy<IPlugin> export = _container.GetExports<IPlugin, IDictionary<string, object>>()
            .Where(c => c.Metadata.ContainsKey("Name")
                && c.Metadata["Namdke"].ToString().ToLowerInvariant().Equals(name.ToLowerInvariant())).
                    First();

        if (export != null)
        {
            _Plugin = export.Value;  // how do I specify a parameter for my constructor?
        }
        else
        {
            throw new MyException(MyExceptionMsgs.E_INVALID_Plugin);
        }
    }

Sep 16, 2009 at 1:02 AM

Hi Joe

We don’t support parameterized construction. We do support constructor injection using the ImportingConstructorAttribute but not passing in the values. If you need to pass in values at construction time, the best way to do it is to either have a property or a method which you can call after the instance has been created.

We are looking into possibly supporting parameterized construction in the future.

Thanks

Glenn

From: joebeazelman [mailto:notifications@codeplex.com]
Sent: Tuesday, September 15, 2009 4:57 PM
To: Glenn Block
Subject: How do I instantiate using Parameterized constructor [MEF:69048]

From: joebeazelman

I want to create a simple plugin model where I want a user to choose 1 plugin among several available plugins located in a directory. This simple task has proven very difficult for me to do. So far, I've been able to locate the plugin and instantiate it, but I can't seem to instantiate it with a parameterized constructor. I am using MEF Preview 7. Here's my code so far:

/// <summary>

/// Selects a plugin based on the name <font size="2">

</font>

/// </summary> <font size="2">

</font>

/// <param name="name">Name of plugin stored in metadata</param>

{

Lazy<IPlugin> export = _container.GetExports<IPlugin, IDictionary<

.Where(c => c.Metadata.ContainsKey(

&& c.Metadata[

First();

{

_Plugin = export.Value; // how do you specify a parameter for the contructor.

}

<font size="2">

</font>

public void FindPluginByName(string name)string, object>>()"Name")"Name"].ToString().ToLowerInvariant().Equals(name.ToLowerInvariant())).if (export != null)else

{

}

}

<font size="2">

</font>

throw new MyException(MyExceptionMsgs.E_INVALID_Plugin);

Read the full discussion online.

To add a post to this discussion, reply to this email (MEF@discussions.codeplex.com)

To start a new discussion for this project, email MEF@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com