gpt4 book ai didi

c# - 编译 protobuf-net 生成的代码时出错

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:00 25 4
gpt4 key购买 nike

protobuf-net 生成的代码中有有趣的错误。 .proto 定义文件包含对象的名为“值”的字段。 protogen.exe 使用 -p:detectMissing 选项为该值生成了什么:

    private int? _value;
[global::ProtoBuf.ProtoMember(50, IsRequired = false, Name=@"value", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
[global::System.Xml.Serialization.XmlElement(@"value", Order = 50)]

public int value
{
get { return _value ?? default(int); }
set { _value = value; }
}

[global::System.Xml.Serialization.XmlIgnore]
[global::System.ComponentModel.Browsable(false)]
public bool valueSpecified
{
get { return _value != null; }
set { if (value == (_value == null)) _value = value ? value : (int?)null; }
}
private bool ShouldSerializevalue() { return valueSpecified; }
private void Resetvalue() { valueSpecified = false; }

编译器认为 value 是关键字而不是类属性会产生错误:

Type of conditional expression cannot be determined because there is no implicit conversion between 'bool' and 'int?'

解决方法是手动更改生成的代码:

public int valueWorkaround
{
get { return _value ?? default(int); }
}

public bool valueSpecified
{
get { return _value != null; }
set { if (value == (_value == null)) _value = value ? valueWorkaround : (int?)null; }
}

然而,修复代码生成也可能有意义吗?

最佳答案

是的,修复代码生成是有意义的。我想,你应该向项目贡献者报告。

关于c# - 编译 protobuf-net 生成的代码时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10182668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com