ActivatorUtilities Class

Helper code for the various activator services.

Namespace
Microsoft.Extensions.DependencyInjection
Assemblies
  • Microsoft.Extensions.DependencyInjection.Abstractions

Syntax

public class ActivatorUtilities
class Microsoft.Extensions.DependencyInjection.ActivatorUtilities

Methods

CreateFactory(System.Type, System.Type[])

Create a delegate that will instantiate a type with constructor arguments provided directly and/or from an System.IServiceProvider.

Arguments:
  • instanceType (System.Type) – The type to activate
  • argumentTypes (System.Type<System.Type>[]) – The types of objects, in order, that will be passed to the returned function as its second parameter
Return type:

Microsoft.Extensions.DependencyInjection.ObjectFactory

Returns:

A factory that will instantiate instanceType using an System.IServiceProvider and an argument array containing objects matching the types defined in argumentTypes

public static ObjectFactory CreateFactory(Type instanceType, Type[] argumentTypes)
CreateInstance(System.IServiceProvider, System.Type, System.Object[])

Instantiate a type with constructor arguments provided directly and/or from an System.IServiceProvider.

Arguments:
  • provider (System.IServiceProvider) – The service provider used to resolve dependencies
  • instanceType (System.Type) – The type to activate
  • parameters (System.Object<System.Object>[]) – Constructor arguments not provided by the <em>provider</em>.
Return type:

System.Object

Returns:

An activated object of type instanceType

public static object CreateInstance(IServiceProvider provider, Type instanceType, params object[] parameters)
CreateInstance<T>(System.IServiceProvider, System.Object[])

Instantiate a type with constructor arguments provided directly and/or from an System.IServiceProvider.

Arguments:
  • provider (System.IServiceProvider) – The service provider used to resolve dependencies
  • parameters (System.Object<System.Object>[]) – Constructor arguments not provided by the <em>provider</em>.
Return type:

T

Returns:

An activated object of type T

public static T CreateInstance<T>(IServiceProvider provider, params object[] parameters)
GetServiceOrCreateInstance(System.IServiceProvider, System.Type)

Retrieve an instance of the given type from the service provider. If one is not found then instantiate it directly.

Arguments:
  • provider (System.IServiceProvider) – The service provider
  • type (System.Type) – The type of the service
Return type:

System.Object

Returns:

The resolved service or created instance

public static object GetServiceOrCreateInstance(IServiceProvider provider, Type type)
GetServiceOrCreateInstance<T>(System.IServiceProvider)

Retrieve an instance of the given type from the service provider. If one is not found then instantiate it directly.

Arguments:provider (System.IServiceProvider) – The service provider used to resolve dependencies
Return type:T
Returns:The resolved service or created instance
public static T GetServiceOrCreateInstance<T>(IServiceProvider provider)