3

I'm trying to use the DryIoc Mef library to scan all assemblies with the Export Attribute

I see this link about Mef with DryIoc

I changed the method to RegisterExports, but the IGreeter property is not being injected. If I register each one by itself it works (Foo/Greeter).

using DryIoc;
using DryIoc.MefAttributedModel;
using System;
using DryIocAttributes;

namespace ConsoleApp3
{
class Program
{
    public static IContainer C;
    static void Main(string[] args)
    {
         Program.C = new Container().With(rules => rules.With( propertiesAndFields: PropertiesAndFields.Auto)).WithMefAttributedModel();

        Program.C.RegisterExports(new Assembly[] { typeof(Foo).GetAssembly() });
        
        var foo = new Foo();
        foo.Message();
        Console.ReadLine();
    }
}

public interface IGreeter
{
    string ShowGreet();
}

[ExportEx]
public class Greeter : IGreeter
{
    public Greeter() { }
    public string ShowGreet()
    {
        return "Hello World";
    }
}

[ExportEx]
public class Foo
{
    public IGreeter greet { get; set; }

    public void Message()
    {
        Program.C.InjectPropertiesAndFields(this);
        Console.WriteLine($"Show {greet.ShowGreet()}");
    }
}

}

Onetx
  • 88
  • 8
Nick Turner
  • 927
  • 1
  • 11
  • 21

2 Answers2

0

Interface IGreeter needed attribute

[InheritedExport]    
public interface IGreeter
{
    string ShowGreet();
}
Nick Turner
  • 927
  • 1
  • 11
  • 21
0

The answer is the change to the ExportEx(typeof(IGreeter)) or to ExportMany. The latter will discover the implemented interfaces and will export those. Also pay attention to the types visibility - ExportMany does not export non-public types by default, but it can be changed with attribute properties.

dadhi
  • 4,807
  • 19
  • 25