ジェネリック基本クラスから実装タイプを取得します。
-
21-09-2019 - |
質問
私は完全に間違ったタイトルを言葉で表現かもしれないが、基本的に、私は次の基本クラスがあります:
をpublic class ScheduleableService<T> : ServiceBase
where T : IJob
{
var x = typeof(T);
}
このの実装はのようなものがあります:
public class MyScheduledService: ScheduleableService<MyScheduledJob>
{
//MyScheduledJob implements IJob
}
基本的に、私は必要なもの、MyScheduledServiceタイプ - を手に入れることができるように、私の基本クラスScheduleableServiceであります 私はそれを通過することなくこれを行うことができますどのような方法があります。
以下の作品は、しかし醜いです.....
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