我有一个在A类中的方法
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继承自类AObject。
B<C> b = new B<C>();
b.mehtodInClassB()
有什么想法吗?
在你的提醒下更新:
是的,我实际想做的是
typeof(AObject).IsAssignableFrom(typeof(T))
不是
typeof(AObject)==typeof(T))
d
的类型是什么?如果它是正确的类型,你不需要传递类型参数(例如d.MyMethod()
),直接调用MyMethod
方法即可。 - Chris