I'm trying to define an interface IScheduler that takes an T1 and returns a T2, and also has a type argument for an IJob<T1,T2> so that it knows what job to create. I'd like to call it as so:
public class SomeJob : IJob<string, int> // preferred way to use the method int result = scheduler.Schedule<SomeJob>("some_param"); and have the compiler infer that T1 is a string and T2 is an int. This is how I've tried defining the interface
public interface IScheduler { T2 Schedule<TJob, T1, T2>(T1 args) where TJob: IJob<T1, T2>; } Unfortunately, the compiler complains:
Using the generic method 'Schedule<TJob,T1,T2>(T1)' requires 3 type arguments The type 'SomeJob' must be convertible to 'Job<T1, T2>' in order to use it as parameter 'TJob` in the generic method 'T2 IScheduler.Schedule<TJob,T1,T2>(T1)' What I'd really like to do is this:
public interface IScheduler { T2 Schedule<TJob>(T1 args) where TJob: IJob<T1, T2>; }
Schedule.<SomeJob>you must define all. compiler doesn't support partially defining generic arguments in any case. second problem is that compiler can not infer the typeintbecauseint resultis just the local variable. what would happen if you just callscheduler.Scheduleand not storing the result anywhere?