1
在下面的代碼,我想打電話,我在派生類中聲明的方法:轉換/投基類型派生泛型類型
class BaseClass
{
// ...
}
class A<T> : BaseClass
{
public void F(T args){ //... }
}
class B<T> : BaseClass
{
// B<T> doesn't have method F()
}
///....
class myApplication
{
// ...
public void DoSomething(BaseClass arg)
{
// Now I know that arg is of type A<T> for some type T
// but I don't know what T is. Also declaring DoSomething
// as DoSomething<T>() is not an option.
//
// I would like to call (arg as A<T>).F(...) but how can I
// deduce T? Can it be done in any other way?
}
}
請閱讀代碼中的註釋。我該如何做這樣的事情?
你試過'class myApplication'? –
假設你有你要傳入F的對象,所以你不知道它的類型? –