Why do I get a “System.StackOverflowException was unhandled ” exception when serializing?

后端 未结 2 1318
悲&欢浪女
悲&欢浪女 2020-12-22 09:59

I\'m trying to serialize the following class:

[Serializable()]
public class BindingNode : IEnumerable
{
    public BindingNode()
    {
            


        
2条回答
  •  有刺的猬
    2020-12-22 10:47

    This would be your problem: BindingNode : IEnumerable This would be recursive and you would encouter a StackOverFlowException. Typically people create two classes.

    The single class:

    public class BindingNode
    {
       /*..*/
    }
    

    The colleciton class:

    public class BindingNodeCollection : IEnumerable
    {
       /*..*/
    }
    

    This approach usually also increases cohesion and satisfies the Single Responsiblity Principle (SRP). It does this by seperating concerts. Collection logic is placed in the collection class and then the original class does what it was entended to do.

提交回复
热议问题