这对我们来说是一个新问题,因为我们更新了proto-net可执行文件和相关文件。这是我们以前没有遇到过的新行为。
在中进行了一些挖掘后,我们找到了“重复”字段的定义:
<xsl:template match="FieldDescriptorProto[label='LABEL_REPEATED']">
<xsl:variable name="type"><xsl:apply-templates select="." mode="type"/></xsl:variable>
<xsl:variable name="format"><xsl:apply-templates select="." mode="format"/></xsl:variable>
<xsl:variable name="field"><xsl:apply-templates select="." mode="field"/></xsl:variable>
private <xsl:if test="not($optionXml)">readonly</xsl:if> global::System.Collections.Generic.List<<xsl:value-of select="$type" />> <xsl:value-of select="$field"/> = new global::System.Collections.Generic.List<<xsl:value-of select="$type"/>>();
[<xsl:apply-templates select="." mode="checkDeprecated"/>global::ProtoBuf.ProtoMember(<xsl:value-of select="number"/>, Name=@"<xsl:value-of select="name"/>", DataFormat = global::ProtoBuf.DataFormat.<xsl:value-of select="$format"/><xsl:if test="options/packed='true'">, Options = global::ProtoBuf.MemberSerializationOptions.Packed</xsl:if>)]<xsl:if test="$optionDataContract">
[global::System.Runtime.Serialization.DataMember(Name=@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>, IsRequired = false)]
</xsl:if><xsl:if test="$optionXml">
[global::System.Xml.Serialization.XmlElement(@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>)]
</xsl:if>
public global::System.Collections.Generic.List<<xsl:value-of select="$type" />> <xsl:call-template name="pascal"/>
{
get { return <xsl:value-of select="$field"/>; }<xsl:if test="$optionXml">
set { <xsl:value-of select="$field"/> = value; }</xsl:if>
}
</xsl:template>
我已经提取了私有字段和setter的具体部分:
private <xsl:if test="not($optionXml)">readonly</xsl:if> ...snip...
public ...snip...
{
...snip...
<!----><xsl:if test="$optionXml">
set { <xsl:value-of select="$field"/> = value; }
</xsl:if>
}
请注意上面针对 $optionXml 的可疑条件。如果您只是删除这些条件,该字段将不再是只读的,并且 setter 将被正确生成。
因此,它变成了:
private ...snip...
public ...snip...
{
...snip...
set { <xsl:value-of select="$field"/> = value; }
}
完整的“固定”模板:
<xsl:template match="FieldDescriptorProto[label='LABEL_REPEATED']">
<xsl:variable name="type"><xsl:apply-templates select="." mode="type"/></xsl:variable>
<xsl:variable name="format"><xsl:apply-templates select="." mode="format"/></xsl:variable>
<xsl:variable name="field"><xsl:apply-templates select="." mode="field"/></xsl:variable>
private global::System.Collections.Generic.List<<xsl:value-of select="$type" />> <xsl:value-of select="$field"/> = new global::System.Collections.Generic.List<<xsl:value-of select="$type"/>>();
[<xsl:apply-templates select="." mode="checkDeprecated"/>global::ProtoBuf.ProtoMember(<xsl:value-of select="number"/>, Name=@"<xsl:value-of select="name"/>", DataFormat = global::ProtoBuf.DataFormat.<xsl:value-of select="$format"/><xsl:if test="options/packed='true'">, Options = global::ProtoBuf.MemberSerializationOptions.Packed</xsl:if>)]<xsl:if test="$optionDataContract">
[global::System.Runtime.Serialization.DataMember(Name=@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>, IsRequired = false)]
</xsl:if><xsl:if test="$optionXml">
[global::System.Xml.Serialization.XmlElement(@"<xsl:value-of select="name"/>", Order = <xsl:value-of select="number"/>)]
</xsl:if>
public global::System.Collections.Generic.List<<xsl:value-of select="$type" />> <xsl:call-template name="pascal"/>
{
get { return <xsl:value-of select="$field"/>; }
set { <xsl:value-of select="$field"/> = value; }
}
</xsl:template>
我尝试将optionXml设置为false,但是它没有起作用,你可能仍然希望启用该选项。
_factoryProperty
上的private readonly
只是意味着即使该类在构建/初始化后也不允许更改该变量。也就是说,不允许将变量_factoryProperty
更改为除该列表以外的任何内容。但是列表本身并不是只读的。 - Jesse Chisholm_factoryProperty
... - Marc Gravell