【问题标题】:Scanning Assemblies with DryIoc使用 DryIoc 扫描程序集
【发布时间】:2021-11-04 21:25:17
【问题描述】:

我正在尝试使用 DryIoc Mef 库来扫描所有具有导出属性的程序集

我看到这个关于 Mef 和 DryIoc 的链接

我将方法更改为 RegisterExports,但未注入 IGreeter 属性。如果我自己注册每一个,它就可以工作(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()}");
    }
}

}

【问题讨论】:

  • 你能看看这个@dadhi
  • 使用 Program.C.Register();

标签: dryioc


【解决方案1】:

接口IGreeter需要的属性

[InheritedExport]    
public interface IGreeter
{
    string ShowGreet();
}

【讨论】:

    【解决方案2】:

    答案是更改为ExportEx(typeof(IGreeter))ExportMany。后者将发现已实现的接口并将其导出。还要注意类型的可见性——ExportMany 默认不导出非公共类型,但可以通过属性属性进行更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-21
      相关资源
      最近更新 更多