Пример #1
0
        /// <summary>
        /// 用DI批量注入接口程序集中对应的实现类。
        /// </summary>
        /// <param name="service"></param>
        /// <param name="interfaceAssemblyName">接口程序集的名称(不包含文件扩展名)</param>
        /// <param name="implementAssemblyName">实现程序集的名称(不包含文件扩展名)</param>
        /// <returns></returns>
        public static IServiceCollection RegisterAssembly(this IServiceCollection service, string interfaceAssemblyName, string implementAssemblyName)
        {
            if (service == null)
            {
                throw new ArgumentNullException(nameof(service));
            }
            if (string.IsNullOrEmpty(interfaceAssemblyName))
            {
                throw new ArgumentNullException(nameof(interfaceAssemblyName));
            }
            if (string.IsNullOrEmpty(implementAssemblyName))
            {
                throw new ArgumentNullException(nameof(implementAssemblyName));
            }

            var interfaceAssembly = RuntimeHelper.GetAssembly(interfaceAssemblyName);

            if (interfaceAssembly == null)
            {
                throw new DllNotFoundException($"the dll \"{interfaceAssemblyName}\" not be found");
            }

            var implementAssembly = RuntimeHelper.GetAssembly(implementAssemblyName);

            if (implementAssembly == null)
            {
                throw new DllNotFoundException($"the dll \"{implementAssemblyName}\" not be found");
            }

            //过滤掉非接口及泛型接口
            //var types = interfaceAssembly.GetTypes().Where(t => t.GetTypeInfo().IsInterface && !t.GetTypeInfo().IsGenericType);
            //过滤掉非接口
            var types = interfaceAssembly.GetTypes().Where(t => t.GetTypeInfo().IsInterface);

            foreach (var type in types)
            {
                ////过滤掉抽象类、泛型类以及非class
                //var implementType = implementAssembly.DefinedTypes
                //    .FirstOrDefault(t => t.IsClass && !t.IsAbstract &&!t.GetTypeInfo()
                //                         t.GetInterfaces().Any(b => b.Name == type.Name));
                //过滤掉抽象类以及非class
                var implementType = implementAssembly.DefinedTypes
                                    .FirstOrDefault(t => t.IsClass && !t.IsAbstract &&
                                                    t.GetInterfaces().Any(b => b.Name == type.Name));
                if (implementType != null)
                {
                    service.AddSingleton(type, implementType.AsType());
                }
            }

            return(service);
        }