2

タイトルは完全に間違っているかもしれませんが、基本的には次の基本クラスがあります。

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
}
4

2 に答える 2

11

this.GetType()または私は何かが欠けていますか?

于 2010-02-19T14:13:54.057 に答える
0
MyScheduledService svc = this as MyScheduledService;
if (svc != null)
{
    // it is a MyScheduledService, and you can work with svc
}

しかし、なぜあなたはこれをする必要があるのでしょうか?それはおそらくあなたのデザインの欠陥を示しています。基本クラスは、派生型について何も知る必要はありません。

于 2010-02-19T14:23:16.050 に答える