-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: ActivatorUtilities.CreateFactory<T>
with params span
#101889
Comments
Tagging subscribers to this area: @dotnet/area-extensions-dependencyinjection |
@steveharter could this be a candidate for API review alongside #101829? |
Yes this is a good proposal; however the window for 9.0 has been closed for such changes. |
The alternative design seems more appealing to me if we do anything here. The goal appears to be to reduce allocations, but We might as well go all the away and do the most optimal thing if we're going to define new CreateFactory overload(s), but I think we should probably just return a |
namespace Microsoft.Extensions.DependencyInjection;
public delegate T ObjectFactoryWithParams<out T>(IServiceProvider serviceProvider, params ReadOnlySpan<object?> arguments);
public static class ActivatorUtilities
{
public static ObjectFactoryWithParams<T> CreateFactory2<T>(params ReadOnlySpan<Type> argumentTypes);
} |
If public static class ActivatorUtilities
{
public static T CreateFactoryOf<T>() where T : Delegate;
} var factory = ActivatorUtilities.CreateFactoryOf<Func<IServiceProvider, SomeType, T>>();
var result = factory(serviceProvider, new SomeType()); This way only one overload would be added. this will avoid array allocation and boxing, but also typechecks that the callsite matches the factory. |
As a result of discussion here I opened up #111228 which I think is more straightforward to implement since that's how ActivatorUtilities already works internally. |
Background and motivation
The arguments passed to
CreateFactory
andObjectFactory
are usually of a fixed number so it may be worthwhile to considerparams span
for both instead of array.API Proposal
I haven't seen an addition like this so far so I'm not sure about the naming convention.
API Usage
Alternative Designs
An alternative is to consider making the return type a generic delegate over argument types.
Risks
No response
The text was updated successfully, but these errors were encountered: