Skip to content

[API Proposal]: ActivatorUtilities.CreateFactoryOf<TDelegate> #111228

Open
@alrz

Description

@alrz

Background and motivation

As an alternative to #101889 this API would be only exposed as a single method taking the delegate as a type arg. This will avoid array allocation and boxing for the args, and also typechecks that the callsite matches the factory.

API Proposal

namespace Microsoft.Extensions.DependencyInjection
{
  public static class ActivatorUtilities
  {
+    public static T CreateFactoryOf<T>() where T : Delegate;
  }
}

API Usage

// IServiceProvider is specially recognized
var f1 = ActivatorUtilities.CreateFactoryOf<Func<IServiceProvider, TArg, TResult>>();
// but it's optional when not using DI
var f2 = ActivatorUtilities.CreateFactoryOf<Func<TArg, TResult>>(); 
// Just like Lambda<T> you could choose to use a custom delegate type
var f3 = ActivatorUtilities.CreateFactoryOf<CustomDelegate>();

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Extensions-DependencyInjectionneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions