作者:陈凯105_462 | 来源:互联网 | 2023-05-29 14:56
1、.net扩展类库,抽象接口和基础类型 案例1:publicstaticclassSample01{publicinterfaceIAccount{publicstringge
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()
.AddScoped()
.AddSingleton()
.BuildServiceProvider();
IAccount entity = provider.GetService();
Console.WriteLine(entity.getDataCZ());
Debug.Assert(provider.GetService() is Account);
Debug.Assert(provider.GetService() is Message);
Debug.Assert(provider.GetService() is Tool);
}
}View Code
输出:
类 ServiceCollection (服务集合)的方法
AddTransient:
AddScoped:
AddSingleton:
作者:chenze
出处:https://www.cnblogs.com/chenze-Index/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
如果文中有什么错误,欢迎指出。以免更多的人被误导。
|
|