Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用.NET核心';s DI容器_C#_Dependency Injection_.net Core - Fatal编程技术网

C# 使用.NET核心';s DI容器

C# 使用.NET核心';s DI容器,c#,dependency-injection,.net-core,C#,Dependency Injection,.net Core,我正在使用IServiceCollection为我的对象创建所需服务的列表。现在我想实例化一个对象,并让DI容器解析该对象的依赖关系 示例 // In my services config. services .AddTransient<IMyService, MyServiceImpl>(); // the object I want to create. class SomeObject { public SomeObject(IMyService servic

我正在使用
IServiceCollection
为我的对象创建所需服务的列表。现在我想实例化一个对象,并让DI容器解析该对象的依赖关系

示例

// In my services config.
services
    .AddTransient<IMyService, MyServiceImpl>();

// the object I want to create.
class SomeObject
{
    public SomeObject(IMyService service)
    {
        ...
    }
}

。。。理由:我不必将所有控制器都添加到服务容器中,因此我也不明白为什么要将
SomeObject
添加到服务容器中

有点粗糙,但这样行得通

public static class ServiceProviderExtensions
    {
        public static TResult CreateInstance<TResult>(this IServiceProvider provider) where TResult : class
        {
            ConstructorInfo constructor = typeof(TResult).GetConstructors()[0];

            if(constructor != null)
            {
                object[] args = constructor
                    .GetParameters()
                    .Select(o => o.ParameterType)
                    .Select(o => provider.GetService(o))
                    .ToArray();

                return Activator.CreateInstance(typeof(TResult), args) as TResult;
            }

            return null;
        }
    }
公共静态类服务提供扩展
{
公共静态TResult CreateInstance(此IServiceProvider提供程序),其中TResult:class
{
ConstructorInfo constructor=typeof(TResult).GetConstructors()[0];
if(构造函数!=null)
{
对象[]args=构造函数
.GetParameters()
.选择(o=>o.ParameterType)
.Select(o=>provider.GetService(o))
.ToArray();
返回Activator.CreateInstance(typeof(TResult),args)作为TResult;
}
返回null;
}
}

如标记答案的注释所述,您可以使用
ActivatorUtilities.CreateInstance
方法。这个功能已经存在于.NET核心中(我相信是从1.0版开始)


请参阅:

要在何处/如何创建对象?@Nkosi在控制台应用程序的主方法中…类似于SomeObject x=startup.ServiceProvider.ResolveDependenciesFor();相关问题:相关文章:使用服务提供者这是如何使用
IServiceProvider
实例化对象(注入所有依赖项)?@alexw.Select(o=>provider.GetService(o))@alexw基本原理,假设有一个构造函数,迭代它的参数类型,使用服务提供商解析它们,然后使用结果创建一个实例。@flodin我写这篇文章已经很久了,但我认为这个功能确实存在。烘焙到.NETCore中的DI实现了服务定位器模式。这将引导你走向正确的方向。。。但是,以.NET核心的方式,他们说它是:var instance=(ipippe)activatureutilities.CreateInstance(serviceProvider,pipeType)。但是您仍然需要一个ServiceProvider对象:-(。我不明白为什么他们让在一个自写类中用DI实例化一个类变得如此困难。
public static class ServiceProviderExtensions
    {
        public static TResult CreateInstance<TResult>(this IServiceProvider provider) where TResult : class
        {
            ConstructorInfo constructor = typeof(TResult).GetConstructors()[0];

            if(constructor != null)
            {
                object[] args = constructor
                    .GetParameters()
                    .Select(o => o.ParameterType)
                    .Select(o => provider.GetService(o))
                    .ToArray();

                return Activator.CreateInstance(typeof(TResult), args) as TResult;
            }

            return null;
        }
    }