我正在尝试使用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()}");
}
}}
发布于 2021-11-04 22:20:11
所需的接口IGreeter属性
[InheritedExport]
public interface IGreeter
{
string ShowGreet();
}发布于 2021-11-05 08:32:38
答案是更改ExportEx(typeof(IGreeter))或ExportMany。后者将发现已实现的接口并将其导出。还要注意类型的可见性-默认情况下,ExportMany不会导出非公共类型,但可以使用属性属性进行更改。
https://stackoverflow.com/questions/69846012
复制相似问题