Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the implemented type from a Generic Base Class

I may of worded the title completely wrong, but basically, I have the following base class:

public class ScheduleableService<T> : ServiceBase
        where T : IJob
{
        var x = typeof(T);
}

The implementation of this is something like:

public class MyScheduledService: ScheduleableService<MyScheduledJob>
{
    //MyScheduledJob implements IJob
}

Basically, what I need, is in my base class ScheduleableService, to be able to get hold of the MyScheduledService type- Is there any way I can do this without passing it in.

The following works, however is ugly.....

public class ScheduleableService<T, S> : ServiceBase
    where T : IJob
    where S : ServiceBase
{

    var x = typeof(T);
    var y = typeof(S);
}

implementation:

public class MyScheduledService: ScheduleableService<MyScheduledJob,
                                                     MyScheduledService>
{
    //MyScheduledJob implements IJob
}
like image 561
Alex Avatar asked May 17 '26 09:05

Alex


1 Answers

this.GetType() or I am missing something?

like image 170
Anton Gogolev Avatar answered May 19 '26 21:05

Anton Gogolev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!