Autofac:注册显式的通用接口
创始人
2024-11-12 13:00:20
0

以下是一个使用Autofac注册显式通用接口的示例代码:

using System;
using Autofac;

public interface IRepository
{
    void Add(T item);
}

public class Repository : IRepository
{
    public void Add(T item)
    {
        Console.WriteLine($"Adding {item} to the repository");
    }
}

public class MyClass
{
    private readonly IRepository _repository;

    public MyClass(IRepository repository)
    {
        _repository = repository;
    }

    public void DoSomething()
    {
        _repository.Add("Hello World");
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        var builder = new ContainerBuilder();

        // 注册显式的通用接口
        builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));

        builder.RegisterType();

        var container = builder.Build();
        var myClass = container.Resolve();

        myClass.DoSomething();
    }
}

在这个示例中,我们定义了一个IRepository接口和一个Repository实现。MyClass依赖于IRepository接口。我们使用builder.RegisterGeneric方法将Repository注册为IRepository的实现。然后,我们使用builder.RegisterType注册MyClass

最后,我们使用container.Resolve方法从容器中解析MyClass实例,并调用DoSomething方法。

当运行这个示例时,输出应该是"Adding Hello World to the repository"。

相关内容

热门资讯

玩家必看教程!pokemmo脚... 玩家必看教程!pokemmo脚本辅助器,hh poker辅助器先试用,果然真的有挂(有挂方针)1、点...
一起来探讨!wepokerpl... 一起来探讨!wepokerplus开挂,德普之星怎么设置埋牌,其实是有挂(有挂助手)一起来探讨!we...
热点推荐!wepoker透视脚... 热点推荐!wepoker透视脚本网页,wepokerplus开挂,确实真的有挂(有挂教程)1、热点推...
玩家必备攻略!wepoker开... 玩家必备攻略!wepoker开脚本视频,hhpoker真能买到挂吗,一直存在有挂(有挂辅助)1、起透...
终于懂了!德扑圈有透视吗,微扑... 终于懂了!德扑圈有透视吗,微扑克数据分析,果然是有挂(有挂教学)1、下载好正确养号方法之后点击打开,...
玩家必备教程!hhpoker德... 玩家必备教程!hhpoker德州作弊,pokermaster脚本,真是存在有挂(证实有挂)运辅助工具...
总算明白!hhpoker德州透... 总算明白!hhpoker德州透视,pokermaster辅助器,总是是有挂(有挂透明挂)一、游戏安装...
一起来探讨!来玩德州破解器,w... 一起来探讨!来玩德州破解器,wepoker作弊视频,都是有挂(有挂秘笈)所有人都在同一条线上,像星星...
实测教程!sohoopoker... 实测教程!sohoopoker辅助,pokemmo脚本,本来是有挂(新版有挂)1、模拟器是什么优化,...
2024教程!pokermas... 2024教程!pokermaster脚本,htx矩阵wepoker辅助,竟然是有挂(讲解有挂)wep...