从一个普通的基类获取实现类型
-
21-09-2019 - |
题
我可以措辞标题的完全错误的,但基本上,我有以下的基类:
public class ScheduleableService<T> : ServiceBase
where T : IJob
{
var x = typeof(T);
}
的此实现是这样的:
public class MyScheduledService: ScheduleableService<MyScheduledJob>
{
//MyScheduledJob implements IJob
}
基本上,我需要的,是在我的基类ScheduleableService,才能够获得MyScheduledService类型 - 的保持 有没有什么办法可以做到这一点没有经过它
下面的作品,但是是难看.....
public class ScheduleableService<T, S> : ServiceBase
where T : IJob
where S : ServiceBase
{
var x = typeof(T);
var y = typeof(S);
}
实现:
public class MyScheduledService: ScheduleableService<MyScheduledJob,
MyScheduledService>
{
//MyScheduledJob implements IJob
}
解决方案
this.GetType()
或者我失去了一些东西?
其他提示
MyScheduledService svc = this as MyScheduledService;
if (svc != null)
{
// it is a MyScheduledService, and you can work with svc
}
但是,为什么你需要做到这一点?它可能指向你的设计缺陷。基类不应该需要知道派生类型什么。
不隶属于 StackOverflow