我有一种在如何投泛型类型,以适应另一个泛型方法
public IList<T> MyMethod<T>() where T:AObject
类我想调用另一个泛型类B.这件T是不受任何约束这种方法。
public mehtodInClassB(){
if (typeof(AObject)==typeof(T))
{
//Compile error here, how can I cast the T to a AObject Type
//Get the MyMethod data
A a = new A();
a.MyMethod<T>();
}
}
C类继承自Class AObject。
B<C> b = new B<C>();
b.mehtodInClassB()
有什么想法?
后URS提醒...更新:
是。我真正想要做的是
typeof(AObject).IsAssignableFrom(typeof(T))
不
typeof(AObject)==typeof(T))
如果我们能看到更多的其他类做什么,我们也许能够提供更多的相关咨询。 –
'd'的类型是什么?如果它是正确的类型,你可以不直接调用MyMethod而不需要传入类型参数(例如'd.MyMethod()')? – Chris
@Chris该方法是通用的,不需要参数。在这种情况下,您不能省略泛型类型参数,因为无法从使用中推断泛型类型参数。 –