So I have a class that implements some concrete generic types. I also has an interface that is implemented by my class. I want to write an extension method that only works on the class if it is implement the generic base class and the interface, and if so resolve type arguments automatically. I tried the following so far:
Here is my extension method:
public static void AddValidationError<TEndpoint, TRequest, TResponse>(this TEndpoint endpoint, string property, string message)
where TEndpoint: Endpoint<TRequest, TResponse>, IValidationError
where TRequest: notnull
{
var container = endpoint.Resolve<IValidationErrorContainer>();
container.Errors.Add(new ValidationInfo(property, message));
}
Here is how I want to use it:
this.AddValidationError(nameof(myclass.Property1), "Message");
This is how it is possible to use it right now:
this.AddValidationError<{type of this}, {concreateT1}, {ConrateT1}>(nameof(myclass.Property1), "Message");
'this' implements Endpoint<(concreteT1), (concreteT1)> and IValidationError, so as you can see, I can figure out TEndpoint, TRequest, TResponse from the context, but I have to be explicit about this.
Is there any way to resolve the type argument without explicitly writing it out? I'm open to other approaches too!