D J
D J

Reputation: 7028

Importing a class without exporting it

I am using MEF. My application is open ended but I still want to hide it from people who are extending it.

e.g. BaseAssembly has

public class ListContainer
{
    [ImportMany(typeof(IBase))]
    public List<IBase> MyObjects { get; set; }

    public void AssembleDriverComponents()
    {
         .... Some code to create catalogue..
         //Crete the composition container
            var container = new CompositionContainer(aggregateCatalog);

            // Composable parts are created here i.e. the Import and Export components assembles here
            container.ComposeParts(this);
    }
}

Other assemblies will refer to base assembly.

ReferenceAssembly will have

[Export(typeof(IBase))]
public class MyDerived
{
    public MyDerived()
    { }
}

I want to avoid this attribute which is on derived class in referenced assembly.

Is it possible?

Upvotes: 1

Views: 66

Answers (1)

Adi Lester
Adi Lester

Reputation: 25201

I think what you're looking for is the InheritedExport attribute. You can use it on your IBase interface, and it will automatically export any class implementing IBase.

[InheritedExport(typeof(IBase))]
public interface IBase
{
    // ...
}

public class MyDerived : IBase
{
    // This class will be exported automatically, as if it had
    // [Export(typeof(IBase))] in its attributes.
}

You can read more about inherited exports here.

Upvotes: 2

Related Questions