VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • 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>>();

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


相关教程
        
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号