C# 堆栈溢出

发布于 2024-07-29 17:02:35 字数 990 浏览 6 评论 0原文

我试图找出为什么会出现堆栈溢出异常。 我正在为学校作业创建一个简单的纸牌游戏,当我克隆纸牌以返回它们时,我收到堆栈溢出异常。

所以我得到了这个卡片类:

public class Card : ICloneable
{
    ....

    #region ICloneable Members

    public object Clone()
    {
        return this.Clone(); // <--- here is the error thrown when the first card is to be cloned
    }

    #endregion
}

并且我有一个名为 Hand 的类,然后它克隆卡片:

internal class Hand
{
        internal List<Card> GetCards()
        {
            return m_Cards.CloneList<Card>(); // m_Cards is a List with card objects
        }
}

最后,我得到了 List 的扩展方法:

    public static List<T> CloneList<T>(this List<T> listToClone) where T : ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }

错误被抛出卡类(IClonable 方法),

CardLibrary.dll 中发生“System.StackOverflowException”类型的未处理异常

I am trying to find out why I am getting a stack overflow exception. I am creating a simple card game for a school assignment and when I clone the cards to return them I get the stack overflow exception.

So I got this card class:

public class Card : ICloneable
{
    ....

    #region ICloneable Members

    public object Clone()
    {
        return this.Clone(); // <--- here is the error thrown when the first card is to be cloned
    }

    #endregion
}

and I have a class called Hand which then clones the cards:

internal class Hand
{
        internal List<Card> GetCards()
        {
            return m_Cards.CloneList<Card>(); // m_Cards is a List with card objects
        }
}

Last, I got an extension method for the List:

    public static List<T> CloneList<T>(this List<T> listToClone) where T : ICloneable
    {
        return listToClone.Select(item => (T)item.Clone()).ToList();
    }

The error gets thrown in the card class (IClonable method),

An unhandled exception of type 'System.StackOverflowException' occurred in CardLibrary.dll

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江城子 2024-08-05 17:02:35

你给自己打电话:

public object Clone()
{
    return this.Clone();
}

这会导致无限递归。

您的 Clone() 方法应该将所有属性/字段复制到一个新对象:

public object Clone()
{
    Card newCard = new Card();

    newCard.X = this.X;
    // ...

    return newCard;
}

或者您可以使用 MemberwiseClone()

public object Clone()
{
    return MemberwiseClone();
}

但这使您对克隆过程的控制较少。

You're calling yourself:

public object Clone()
{
    return this.Clone();
}

This results in infinite recursion.

Your Clone() method should copy all properties/fields to a new object:

public object Clone()
{
    Card newCard = new Card();

    newCard.X = this.X;
    // ...

    return newCard;
}

or you could use MemberwiseClone()

public object Clone()
{
    return MemberwiseClone();
}

But that gives you less control over the cloning process.

此岸叶落 2024-08-05 17:02:35

我倾向于对简单数据使用 MemberwiseClone() ,然后在我需要克隆的元素层次结构中实现 ICloneable ,因此:

public class CRMLazyLoadPrefs : ICloneable
{
    public bool Core { get; set; }
    public bool Events { get; set; }    
    public bool SubCategories { get; set; }
    public OrganisationLazyLoadPrefs { get; set; }

    public object Clone()
    {
        CRMLazyLoadPrefs _prefs = new CRMLazyLoadPrefs();
        // firstly, shallow copy the booleans
        _prefs  = (CRMLazyLoadPrefs)this.MemberwiseClone();
        // then deep copy the other bits
        _prefs.Organisation = (OrganisationLazyLoadPrefs)this.Organisation.Clone();
    }
}

其中 OrganizationLazyLoadPrefs 也在整个层次结构中实现 ICloneable 等等。

希望这可以帮助,
干杯,
特里

I've tended to use MemberwiseClone() for the simple data, and then implemented ICloneable throghout the hierarchy of elements that I've needed to clone, so:

public class CRMLazyLoadPrefs : ICloneable
{
    public bool Core { get; set; }
    public bool Events { get; set; }    
    public bool SubCategories { get; set; }
    public OrganisationLazyLoadPrefs { get; set; }

    public object Clone()
    {
        CRMLazyLoadPrefs _prefs = new CRMLazyLoadPrefs();
        // firstly, shallow copy the booleans
        _prefs  = (CRMLazyLoadPrefs)this.MemberwiseClone();
        // then deep copy the other bits
        _prefs.Organisation = (OrganisationLazyLoadPrefs)this.Organisation.Clone();
    }
}

Where OrganisationLazyLoadPrefs also implements ICloneable and so on and so forth throughout the hierarchy.

Hope this helps,
Cheers,
Terry

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文