admin管理员组

文章数量:1122832

I want to ensure that my IDE, during autocomplete for function parameters, suggests only (or at least prioritizes) the parameters that are allowed in the function. Specifically, this pertains to an extension function for the MediatR library. The following example illustrates the problem:


using MediatR;

namespace ITaskTest;

public class Result<T> {
    public T Value { get; set; }
    public string ErrorMessage { get; set; } 
}

public class Result { }


public interface ITask : IRequest<Result>;

public interface ITask<TResponse> : IRequest<Result<TResponse>>;


public record TestTask : ITask;

public record TestTaskGeneric : ITask<TestTaskGenericResponse>;
public record TestTaskGenericResponse(string Message);


public static class MediatorExtensions
{
    public static async Task<Result> RunTask(this IMediator mediator, ITask task) {
        return await mediator.Send(task);
    }

    public static async Task<Result<TResponse>> RunTaskGeneric<TResponse>(this IMediator mediator, ITask<TResponse> task) {
        return await mediator.Send(task);
    }
}

public class Class1
{
    private readonly IMediator mediator;

    public Class1(IMediator mediator) {
        this.mediator = mediator;
    }

    public async Task Test() {
        // IDE should suggest TestTask here -> does work!
        var result = await mediator.RunTask(new TestTask());

        // IDE should suggest TestTaskGeneric here -> does not work!
        var resultGeneric = await mediator.RunTaskGeneric(new TestTaskGeneric());
    }
}

Also see images for example.

does work

does not work

The reason why this makes sense: I have a large number of ITask implementations as well as some other interfaces like IQuery and ICommand. During development, I want to limit the selection of options. Additionally, it doesn't make sense for options like TestTaskGenericResponse to be suggested as a possible parameter. The IDE even immediately points out that it’s not a valid parameter but still suggests it.

Am I missing something here? Or is something like this generally not possible?

I’ve tried various configurations and asked ChatGPT multiple times, but I haven’t been able to find a solution.

本文标签: