You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用DryIoc.Mef扫描程序集后IGreeter属性无法注入问题求助

问题根因
  • 你给Greeter类标记的[ExportEx]特性未显式指定导出的服务类型,默认只会把Greeter自身作为服务类型注册到容器,不会关联到IGreeter接口,所以容器中找不到IGreeter的实现,自然无法完成属性注入。
  • 手动注册时你显式指定了服务类型为IGreeter、实现类型为Greeter,所以可以正常运行。
修复方案

两种方案任选其一即可:

方案1:修改ExportEx特性显式指定导出接口

直接给Greeter类的导出特性指定服务类型为IGreeter,修改后代码如下:

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

方案2:配置扫描规则自动导出实现类的所有接口

调用RegisterExports时传入注册配置,让扫描到的类型自动导出自身、以及自身实现的所有接口作为服务类型,和手动注册的效果完全一致:

Program.C.RegisterExports(new Assembly[] { typeof(Foo).GetAssembly() },
    type => type.GetExportedInterfaces().Select(i => new ServiceTypeInfo(i))
        .Append(new ServiceTypeInfo(type)));

内容的提问来源于stack exchange,提问作者Nick Turner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 00:36:04