无法访问受保护的成员'object.MemberwiseClone() [英] Cannot access protected member 'object.MemberwiseClone()'

查看:319
本文介绍了无法访问受保护的成员'object.MemberwiseClone()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用 .MemberwiseClone()在自定义类矿井,但它抛出了这个错误:

I'm trying to use .MemberwiseClone() on a custom class of mine, but it throws up this error:

Cannot access protected member 'object.MemberwiseClone()' via a qualifier of type 'BLBGameBase_V2.Enemy'; the qualifier must be of type 'BLBGameBase_V2.GameBase' (or derived from it)



这是什么意思?或者更好的,我怎么能克隆敌人类?

推荐答案

在任何类 X ,你只能叫 MemberwiseClone (或任何其他受保护的方法)对<$ C的一个实例$ C> X 。 (或者从 X衍生的类

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)

由于敌人,你想克隆不继承你想在克隆它的 GameBase 类类,你收到此错误。

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(); }
}

这篇关于无法访问受保护的成员'object.MemberwiseClone()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆