Dependency injection 在.NET Core中自动解析没有配置的具体类?
我正在将一个项目从.NET4.52移植到.NETCore。此项目以前使用Structuremap进行依赖项注入,在Structuremap中,您不需要配置具体类型来启用依赖项注入。对于.NET Core中的内置依赖项注入,有什么方法可以做到这一点吗?如果您试图解析一个具体类型,并从IoC容器中注入其依赖项,那么下面的扩展函数可能对您有用。这就假设所有具体类型的依赖关系都可以通过容器来解决Dependency injection 在.NET Core中自动解析没有配置的具体类?,dependency-injection,asp.net-core,Dependency Injection,Asp.net Core,我正在将一个项目从.NET4.52移植到.NETCore。此项目以前使用Structuremap进行依赖项注入,在Structuremap中,您不需要配置具体类型来启用依赖项注入。对于.NET Core中的内置依赖项注入,有什么方法可以做到这一点吗?如果您试图解析一个具体类型,并从IoC容器中注入其依赖项,那么下面的扩展函数可能对您有用。这就假设所有具体类型的依赖关系都可以通过容器来解决 using System; using System.Collections.Generic; using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceProviderExtensions
{
public static TService AsSelf<TService>(this IServiceProvider serviceProvider)
{
return (TService)AsSelf(serviceProvider, typeof(TService));
}
public static object AsSelf(this IServiceProvider serviceProvider, Type serviceType)
{
var constructors = serviceType.GetConstructors(BindingFlags.Public | BindingFlags.Instance)
.Select(o => o.GetParameters())
.ToArray()
.OrderByDescending(o => o.Length)
.ToArray();
if (!constructors.Any())
{
return null;
}
object[] arguments = ResolveParameters(serviceProvider, constructors);
if (arguments == null)
{
return null;
}
return Activator.CreateInstance(serviceType, arguments);
}
private static object[] ResolveParameters(IServiceProvider resolver, ParameterInfo[][] constructors)
{
foreach (ParameterInfo[] constructor in constructors)
{
bool hasNull = false;
object[] values = new object[constructor.Length];
for (int i = 0; i < constructor.Length; i++)
{
var value = resolver.GetService(constructor[i].ParameterType);
values[i] = value;
if (value == null)
{
hasNull = true;
break;
}
}
if (!hasNull)
{
// found a constructor we can create.
return values;
}
}
return null;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
运用系统反思;
使用系统文本;
命名空间Microsoft.Extensions.DependencyInjection
{
公共静态类服务提供扩展
{
公共静态TService AsSelf(此IServiceProvider服务提供程序)
{
return(TService)AsSelf(serviceProvider,typeof(TService));
}
公共静态对象AsSelf(此IServiceProvider服务提供程序,类型为serviceType)
{
var constructors=serviceType.GetConstructors(BindingFlags.Public | BindingFlags.Instance)
.Select(o=>o.GetParameters())
.ToArray()
.OrderByDescending(o=>o.Length)
.ToArray();
if(!constructor.Any())
{
返回null;
}
对象[]参数=ResolveParameters(serviceProvider、构造函数);
if(参数==null)
{
返回null;
}
返回Activator.CreateInstance(serviceType,参数);
}
私有静态对象[]ResolveParameters(IServiceProvider解析器、ParameterInfo[]构造函数)
{
foreach(构造函数中的ParameterInfo[]构造函数)
{
bool hasNull=false;
object[]value=新对象[constructor.Length];
for(int i=0;i
如果需要,您仍然可以使用结构图。是的,但是我想尽可能地使用原生的,这似乎是他们应该添加的,或者你确定这是不可能的吗?坚持核心DI库。正如@Steven所说,原生DI并不意味着功能丰富并取代其他DI系统。它的目的是作为一个基础,在这里可以插入其他DI库,并为较小的项目提供一个好的、简单的DI,开始时w/o必须依赖第三方。因此,没有计划添加自动发现(注册特定类型的所有类或直接解析具体类型)或具有更高级/复杂应用程序所需的拦截器/装饰器(即使用记录器或缓存装饰器装饰类/接口),谢谢您的评论。将希望将代码还原回structuremap,然后:)