I\'m trying to use .MemberwiseClone()
on a custom class of mine, but it throws up this error:
Cannot access protected member \'object.Memberwise
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(); }
}