这是一个相当初级的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