Skip to content

Make DecorationStrategy.WithType Public #248

@dumbnessrf

Description

@dumbnessrf

Actually, when I want to decorate specified service by key string, there is no way to do that, but I noticed DecorationStrategy.WithType method can create DecorationStrategy and i can decorate by key through method Decorate by overload. But I can only do that by reflection. Please consider making WithType to public accessibility

public static IServiceCollection DecorateKeyedService<TService, TDecorator>(
    this IServiceCollection services,
    string key
)
{
    var method = typeof(DecorationStrategy).GetMethod(
        "WithType",
        System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic
    );
    if (method == null)
    {
        throw new InvalidOperationException("DecorationStrategy.WithType method not found");
    }
    var obj =
        method?.Invoke(
            null,
            [typeof(TService), key, typeof(TDecorator)]
        ) as DecorationStrategy;
    if (obj == null)
    {
        throw new InvalidOperationException("DecorationStrategy.WithType invocation failed");
    }
    services.Decorate(obj);
    return services;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions