在反序列化时初始化构造函数内部的属性



我使用Xsd2Code生成类。然而,其中一个类的构造函数出现了问题,因为循环并引发了StackOverflowException。

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.32990")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class ApproverType : INotifyPropertyChanged
{
    private ApproverType replacesField;
    public ApproverType()
    {
        this.replacesField = new ApproverType();
    }
    public ApproverType Replaces
    {
        get
        {
            return this.replacesField;
        }
        set
        {
            if ((this.replacesField != null))
            {
                if ((replacesField.Equals(value) != true))
                {
                    this.replacesField = value;
                    this.OnPropertyChanged("Replaces");
                }
            }
            else
            {
                this.replacesField = value;
                this.OnPropertyChanged("Replaces");
            }
        }
    }
}

如何在XSD中定义Replaces属性?可能它是必需的元素,并且该工具生成一个实例以符合约定。

然后,您可以尝试更改定义,使其成为可选的。

如果是类似于:

<xs:element name="Replaces" type="ApproverType" use="required" />

然后您必须将其更改为:

<xs:element name="Replaces" type="ApproverType" use="optional" />

编辑:无论如何,一个方案都是不正确的,因为在xml中递归所需的元素也是不可能的:

<ApproverType>
 <Replaces>
   <Replaces>
     <Replaces>
       <Replaces>
        ... infinite
 <Replaces>
 <OtherProperty />
<ApproverType>

编辑:

一种可能的解决方法是将后备属性作为单个元素的集合:

public List<ApproverType> ReplacesWorkaround { ... }

在分部类的另一部分中:

public ApproverType Replaces
{
 get
 {
  return ReplacesWorkaround.SingleOrDefault();
 }
 set
 {
  ReplacesWorkaround.RemoveAll();
  ReplacesWorkaround.Add(value);
 }
}

问题是还有其他类似的类。。很明显,这个工具有问题。https://xsd2code.codeplex.com/workitem/7419

我使用的是版本3.4.0.32990。有没有办法解决这个问题,很容易

最新更新