這是一個相當初級的C#問題;我的道歉,但我一直無法在其他地方找到。C#類型轉換爲泛型參數
什麼是從Object<class>
轉換爲Object<interface>
的最佳方法?
I.e.
//fake interface
interface ignu {}
//fake class which implements fake interface
class bee : ignu {}
//function which accepts a generic with the interface
function bang(template<bee>) {}
//template type but with a class that implements the interface
template<bar> foo;
//trying to call the function, but compiler complains it can't implicitly convert
bang(foo);
//compilers complains that it cannot convert from template<bee> to template<ignu>
bang((template<ignu>)bee)
也許我是遠離基地,但這似乎應該工作和可行,但解決方案是逃避我。
編輯:mygen更改爲模板,我用兩指這是混淆
編輯同樣的事情:我結束了使用強制類似於: 邦(bee.Cast());
'mygen'應該是'template'嗎? – recursive 2010-08-24 18:04:21
我對bang的簽名感到困惑,你是否試圖說它接受一個任意的類? – MerickOWA 2010-08-24 18:10:57