检查接口是否在编译时由类型实现

Sim*_*röm 0 .net c# asp.net

我有一个项目,我希望保留Type对该类型的实例和后来的初始化实例.但我想要一些编译时类型检查,所以我只能提供实现ITest接口的类型.我想我必须改变方法,但我看不出如何.

private static Type currentType = null;

public static void Initalize (Type current){
     currentType = current;
}

public class Test : ITest{}
public class Test2 {}
Run Code Online (Sandbox Code Playgroud)

应该可以通过typeof(Test)而不是通过typeof(Test2)

Dan*_*ite 5

为什么不使用泛型?

private static Type currentType = null;

public static void Initalize <T>() where T: ITest {
     currentType = typeof(T);
}
Run Code Online (Sandbox Code Playgroud)