有人知道如何确保Type的值继承自超类的Type吗?
public class Resource {
private Executor executor = Executors.newSingleThreadExecutor();
@GET
public void asyncGet(@Suspended final AsyncResponse asyncResponse) {
asyncResponse.setTimeoutHandler(new TimeoutHandler() {
@Override
public void handleTimeout(AsyncResponse asyncResponse) {
asyncResponse.resume("Processing timeout.");
executor.shutdown();
}
});
asyncResponse.setTimeout(60, TimeUnit.SECONDS);
executor.submit(() -> {
String result = someService.expensiveOperation();
asyncResponse.resume(result);
executor.shutdown();
});
}
}
我知道如何在Java中使用:
public class MyClass {
private Type myType; // where the type is a subclass MySuperClass type
}
但是我想知道如何在C#中做到这一点
答案 0 :(得分:0)
您应在以下位置使用
:class Myclass<Type> where Type: Superclass
答案 1 :(得分:0)
要回复Skaparate,这不正是我想要的。我不想在我想使用Types作为属性(例如类型列表的例子)时被迫创建一个新类
所以我创建了一个类似于java.lang.Class的类,我将在需要时添加任何方法:
public class Class<T> where T : class {
public T newInstance()
{
return (T) Activator.CreateInstance(typeof(T));
}
public T newInstance(params object[] parameters)
{
return (T) Activator.CreateInstance(typeof(T), parameters);
}
public new Type GetType()
{
return typeof(T);
}
}
public abstract class Spell {
// as a battle animation's attributes changes during the animation I want to recreate a new one anytime this spell is used
private Class<BattleAnimation> animation;
public BattleAnimation GetAnimation() {
return this.animation.newInstance();
}
}