MEF Constructor Parameters with Multiple Constructors

后端 未结 2 1532
终归单人心
终归单人心 2021-01-12 07:04

I\'m starting to use MEF, and I have a class with multiple constructors, like this:

[Export(typeof(ifoo))]
class foo : ifoo {
    void foo() { ... }
    [Imp         


        
相关标签:
2条回答
  • 2021-01-12 07:34

    MEF should use the constructor you put the ImportingConstructorAttribute on. I'm not sure what is happening for you, I wasn't able to reproduce the issue. Here is a test which shows using an ImportingConstructor on a class that also has a default constructor:

    [TestClass]
    public class MefTest
    {
        public const string ConstructorParameterContract = "FooConstructorParameterContract";
    
        [TestMethod]
        public void TestConstructorInjectionWithMultipleConstructors()
        {
            string ExpectedConstructorParameterValue = "42";
    
            var catalog = new TypeCatalog(typeof(Foo), typeof(FooImporter));
            var container = new CompositionContainer(catalog);
    
            container.ComposeExportedValue<string>(ConstructorParameterContract, ExpectedConstructorParameterValue);
    
            var fooImporter = container.GetExportedValue<FooImporter>();
    
            Assert.AreEqual(1, fooImporter.FooList.Count, "Expect a single IFoo import in the list");
            Assert.AreEqual(ExpectedConstructorParameterValue, fooImporter.FooList[0].Value.ConstructorParameter, "Expected foo's ConstructorParameter to have the correct value.");
        }
    }
    
    public interface IFoo
    {
        string ConstructorParameter { get; }
    }
    
    [Export(typeof(IFoo))]
    public class Foo : IFoo
    {
        public Foo()
        {
            ConstructorParameter = null;
        }
    
        [ImportingConstructor]
        public Foo([Import(MefTest.ConstructorParameterContract)]string constructorParameter)
        {
            this.ConstructorParameter = constructorParameter;
        }
    
    
        public string ConstructorParameter { get; private set; }
    }
    
    [Export]
    public class FooImporter
    {
        [ImportMany]
        public List<Lazy<IFoo>> FooList { get; set; }
    }
    
    0 讨论(0)
  • 2021-01-12 07:40

    Are you passing an instance of the foo class into the ComposeExportedValue method? In that case the object has already been constructed and the constructor can't be called again, so MEF will ignore the constructor imports.

    0 讨论(0)
提交回复
热议问题