Skip to content

Inject existing object into MEF2 #27324

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gthvidsten opened this issue Sep 6, 2018 · 2 comments
Closed

Inject existing object into MEF2 #27324

gthvidsten opened this issue Sep 6, 2018 · 2 comments
Labels
area-System.Composition question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@gthvidsten
Copy link

Say I have an object:

IMyObject obj = SomeFactory.CreateMyObject();
obj.NeedsSomeConfiguration()
    .NeedsMoreConfiguration();

and I've set up MEF to use the current assembly (of which IMyObject is defined)

ContainerConfiguration containerConfig = new ContainerConfiguration()
    .WithAssembly(Assembly.GetExecutingAssembly())

How can I inject the specific instance of obj into this configuration so that I can do the following in an exported class

[Export(typeof(IOtherObject))]
public class OtherObject : IOtherObject
{
    [Import] public IMyObject MyObject { get; set; }
}
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the Future milestone Jan 31, 2020
@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 23, 2020
@CZEMacLeod
Copy link

I'm fairly sure this is partially answered in #29400 by @nblumhardt
The 4 files in Microsoft.Composition.Demos.ExtendedPartTypes give you the ability.
It would be nice if these were bundled up and either included in one of the existing System.Composition.* packages or a new System.Composition.??? package.

Regardless, with the extensions available you can do

ContainerConfiguration containerConfig = new ContainerConfiguration()
    .WithAssembly(Assembly.GetExecutingAssembly())
    .WithExport<IMyObject>(obj);

or even

ContainerConfiguration containerConfig = new ContainerConfiguration()
    .WithAssembly(Assembly.GetExecutingAssembly())
    .WithFactoryDelegate<IMyObject>(() => {
        IMyObject obj = SomeFactory.CreateMyObject();
        obj.NeedsSomeConfiguration()
            .NeedsMoreConfiguration();
        return obj;
    });

or maybe

    .WithFactoryDelegate<IMyObject>(() => SomeFactory.CreateMyObject()
        .NeedsSomeConfiguration()
        .NeedsMoreConfiguration());

@tarekgh
Copy link
Member

tarekgh commented Apr 14, 2022

Closing as dups of #29400 (comment)

@tarekgh tarekgh closed this as completed Apr 14, 2022
Repository owner moved this from Future to Done in Triage POD for Reflection, META, etc. Apr 14, 2022
@ghost ghost locked as resolved and limited conversation to collaborators May 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Composition question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
No open projects
Development

No branches or pull requests

6 participants