-
C#教程之动手造轮子:实现一个简单的依赖注入(一)(2)
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
/// ResolveRequiredService /// throw exception if can not get a service instance /// </summary> /// <typeparam name="TService">TService</typeparam> /// <param name="serviceProvider">serviceProvider</param> /// <returns></returns> public static TService ResolveRequiredService<TService>([NotNull] this IServiceProvider serviceProvider) { var serviceType = typeof(TService); var svc = serviceProvider.GetService(serviceType); if (null == svc) { throw new InvalidOperationException($"service had not been registered, serviceType: {serviceType}"); } return (TService)svc; } /// <summary> /// Resolve services /// </summary> /// <typeparam name="TService">TService</typeparam> /// <param name="serviceProvider">serviceProvider</param> /// <returns></returns> public static IEnumerable<TService> ResolveServices<TService>([NotNull]this IServiceProvider serviceProvider) => serviceProvider.ResolveService<IEnumerable<TService>>();
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
/// ResolveRequiredService /// throw exception if can not get a service instance /// </summary> /// <typeparam name="TService">TService</typeparam> /// <param name="serviceProvider">serviceProvider</param> /// <returns></returns> public static TService ResolveRequiredService<TService>([NotNull] this IServiceProvider serviceProvider) { var serviceType = typeof(TService); var svc = serviceProvider.GetService(serviceType); if (null == svc) { throw new InvalidOperationException($"service had not been registered, serviceType: {serviceType}"); } return (TService)svc; } /// <summary> /// Resolve services /// </summary> /// <typeparam name="TService">TService</typeparam> /// <param name="serviceProvider">serviceProvider</param> /// <returns></returns> public static IEnumerable<TService> ResolveServices<TService>([NotNull]this IServiceProvider serviceProvider) => serviceProvider.ResolveService<IEnumerable<TService>>();
More#
后面还更新了一版,主要优化性能,目前来说还不太满意,暂时这里先不提了
Reference#
- Dynamic Casting using Reflection
- https://github.com/WeihanLi/WeihanLi.Common/blob/dev/test/WeihanLi.Common.Test/DependencyInjectionTest.cs
作者: WeihanLi
出处:https://www.cnblogs.com/weihanli/p/implement-dependency-injection-01.html
栏目列表
最新更新
记录一次排查使用HttpWebRequest发送请求的
动手造轮子:实现一个简单的依赖注入
C# 添加、修改、删除Excel图表数据标签
C#利用newtonsoft.json读取.so配置文件内容
Quartz.Net 删除一个Job
DevExpress的TextEdit控件没法调整高度解决
C#特性
SQLserver、MySQL、ORCAL查询数据库、表、表中
C# 利用AForge进行摄像头信息采集
DataGridView中的rows.Count比实际行数多1的原
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
Dubbo(五):深入理解Dubbo核心模型Invok
vfp教程之VFP与Excel交互编程
vfp教程之在VFP中实现跟变式组合框及椭圆
SQL SERVER查询数据库所有表的大小,按照记
使用 SQL 服务器时,"评估期已过期"错
sql server无法连接本地服务器
使用sql语句创建表
VB操作Access数据库小记 ————————
access数据库远程连接
java web操作Access数据库