1、
.net扩展类库,抽象接口和基础类型
案例1:
public static class Sample01 { public interface IAccount{ public string getDataCZ(); } public interface IMessage{ } public interface ITool{ } public class Account: IAccount{ public string getDataCZ() { return "CHENZE 第一个依赖框架"; } } public class Message: IMessage{} public class Tool: ITool{} public static void Run() { var provider = new ServiceCollection() .AddTransient<IAccount, Account>() .AddScoped<IMessage, Message>() .AddSingleton<ITool, Tool>() .BuildServiceProvider(); IAccount entity = provider.GetService<IAccount>(); Console.WriteLine(entity.getDataCZ()); Debug.Assert(provider.GetService<IAccount>() is Account); Debug.Assert(provider.GetService<IMessage>() is Message); Debug.Assert(provider.GetService<ITool>() is Tool); } }View Code
输出:
类 ServiceCollection (服务集合)的方法
AddTransient:
AddScoped:
AddSingleton:
标签:依赖,框架,public,Assert,provider,Debug,GetService,class,注入 From: https://www.cnblogs.com/chenze-Index/p/16813275.html