Within any class X
, you can only call MemberwiseClone
(or any other protected method) on an instance of X
. (Or a class derived from X
)
Since the Enemy
class that you're trying to clone doesn't inherit the GameBase
class that you're trying to clone it in, you're getting this error.
To fix this, add a public Clone
method to Enemy
, like this:
class Enemy : ICloneable {
//...
public Enemy Clone() { return (Enemy)this.MemberwiseClone(); }
object ICloneable.Clone() { return Clone(); }
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…