混淆和 protobuf.net - 异常:未定义默认枚举值

发布于 2024-07-22 09:14:01 字数 557 浏览 10 评论 0原文

当我尝试序列化混淆项目中包含枚举的类时,出现以下异常:

ProtoBuf.ProtoException:未为可选属性 Y 定义默认枚举值 X

如果我从混淆中排除所有受影响的枚举,则一切运行正常,但是,我切换到 protobuf.net 以便能够混淆更多代码内容,所以我希望有更好的解决方案。

那么,是混淆器给 protobuf.net 带来了太多混乱,还是我声明枚举的方式错误了?

我已经尝试过:

    [ProtoContract]
    public enum X
    {
        Y, Z
    }

而且

    [ProtoContract]
    public enum X
    {
        Y=0, Z=1
    }

根本没有合同,还有其他一些不那么明显的事情,但除了排除之外没有任何作用。 顺便说一句:在使用 protobuf.net 时,是否有一个我们必须与枚举相关的示例?

I'm being presented with the following Exception when trying to serialize a class that contains enums in an obfuscated project:

ProtoBuf.ProtoException: The default enum value X is not defined for the optional property Y

If I exclude all affected enums from obfuscation everything runs fine, however, I switched to protobuf.net to be able to obfuscate more code content so I hope there is a better solution.

So is it the obfuscator which messes around to much for protobuf.net or am I declaring my enums the wrong way?

I have tried:

    [ProtoContract]
    public enum X
    {
        Y, Z
    }

and

    [ProtoContract]
    public enum X
    {
        Y=0, Z=1
    }

also without a contract at all and several other not so obvious things but nothing except exclusion works. By the way: Is there an example somewhere what we have to do with enums when using protobuf.net?

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

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

发布评论

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

评论(1

赤濁 2024-07-29 09:14:01

嗯……老实说,我不知道枚举的混淆问题; 我必须准备一个测试用例来进行调查。

如果您能告诉我您正在使用什么混淆工具,将会有所帮助。 了解如何指定默认值(即属性定义)也将有所帮助。

请注意,它仅在枚举的情况下真正考虑 [ProtoEnum][ProtoContract] 可用于为其指定名称,但除非您正在生成 .proto 文件,这不太可能) - 但我不希望它在这种情况下产生任何影响(这用于将“在线”值更改为与 .NET 中不同的值)。 至于例子; 我承认我在文档上落后了 - 但是

我已将此记录为第 59 期; 如果您能让我知道上述详细信息(在此处或给我发送电子邮件 - 查看我的个人资料),我会尝试进行调查。

(如果你不知道,我是 protobuf-net 的作者)


我尝试了以下方法(使用 .NET Reactor)并且运行良好......枚举值的隐式默认值零是最有可能的嫌疑。 您能提供一个显示失败的测试用例吗?

using System;
using ProtoBuf;

[ProtoContract]
class Foo {
    static void Main() {
        Foo foo = new Foo { Bar = MyEnum.B };
        Console.WriteLine(foo.Bar);
        Foo clone = Serializer.DeepClone(foo);
        Console.WriteLine(clone.Bar); // Expect "B"
    }

    [ProtoMember(1)]
    public MyEnum Bar { get; set; }
}
enum MyEnum { A, B, C }

Hmmm.... I'm honestly not aware of obfuscation issues with enums; I will have to prepare a test case to investigate.

It would help if you could tell me what obfuscation tool you are using. It would also help to see how you are specifying the default value (i.e. the property definition).

Note that it only really considers [ProtoEnum] in the case of enums (the [ProtoContract] can be used to give it a name, but this isn't used unless you are generating .proto files, which is very unlikely) - but I don't expect it to impact anything in this case (this is used to change the value "on the wire" to different values than are in .NET). As for examples; I confess I'm behind on the documentation - but the enum test cases here show typical usage.

I've logged this as Issue 59; if you could let me know the details above (either here, or e-mail me - see my profile), I'll try to investigate.

(if you didn't know, I'm the author of protobuf-net)


I tried the following (using .NET Reactor) and it worked fine... the implicit default of zero on enum values is the most likely suspect. Can you supply a test-case that shows it failing?

using System;
using ProtoBuf;

[ProtoContract]
class Foo {
    static void Main() {
        Foo foo = new Foo { Bar = MyEnum.B };
        Console.WriteLine(foo.Bar);
        Foo clone = Serializer.DeepClone(foo);
        Console.WriteLine(clone.Bar); // Expect "B"
    }

    [ProtoMember(1)]
    public MyEnum Bar { get; set; }
}
enum MyEnum { A, B, C }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文