2

下面的代码是我用于 java 程序将泛型转换为特定的代码。我知道 C# 中没有通配符,但希望有人能指出正确的方向,因为我可以将代码修改为类似于 java:

public <T extends GameComponent<?>>T getComponent(int id, Class<T> type){
    return type.cast(components.get(id));
}
4

1 回答 1

4

看起来像:

public T GetComponent<T>(int id) where T : GameComponent
{
    return components[id] as T;
}

感谢评论中的每个人,帮助我解决我的解决方案(因为我很遥远)。没有你们,我不可能在这里。我会想念你的。

<3

但真的,谢谢 :D

于 2012-06-03T19:27:20.090 回答