使用 .Net OnSerializingAttribute 更改变量名称?

发布于 2024-07-17 00:38:18 字数 343 浏览 7 评论 0 原文

为了混淆应用程序代码但仍然能够使用序列化来保存应用程序数据,我想将所有序列化成员的名称映射到自定义序列化名称。 我知道我可以通过实现 ISerizableInterface 来实现此目的,但在 MSDN 中,他们建议使用 OnDeserializedAttribute、OnSerializingAttribute、OnSerializedAttribute 和 OnDeserializingAttribute 进行序列化。 所以我想使用 OnSerializingAttribute 来重命名我的成员。 问题是我找不到一种方法来显式确定序列化成员的名称。

是否可以使用 OnSerializingAttribute 显式定义序列化名称?

in order to obfuscate application code but still be able to use serialization to save application data, I want to map the names of all serialized members to custom serialization names. I know that I can achieve this by implementing the ISerizableInterface, but in the MSDN they suggest the use of OnDeserializedAttribute, OnSerializingAttribute, OnSerializedAttribute, and OnDeserializingAttribute for serialization. So I would like to use the OnSerializingAttribute to rename my members. The problem is that I can't find a way to explicitly determine the names under which a member is serialized.

Is it possible to explicitly define the names for serialization using the OnSerializingAttribute?

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

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

发布评论

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

评论(3

南风起 2024-07-24 00:38:18

我认为自定义序列化和 ISerialized 接口将是您的情况下最有效的方法。 请查看此处的示例。 另外这里 针对您提到的 ISerialized 和序列化属性的一个很好的讨论线程。

我想说,如果您想完全控制格式,您应该使用自定义序列化程序,如果您想响应(反)序列化流程,则属性是最佳选择。

I think custom serialization and ISerializable interface will be the most efficient way in your case. Check out the sample here. Also here's a good discussion thread towards both ISerializable and serialization attributes you've mentioned.

I would say that if you want to have a full control over the format you should use custom serializer and if you want responding the (de)serialization flow the attributes is the best option.

故乡的云 2024-07-24 00:38:18

编辑:我决定将其写为 博客条目,因为我已经多次看到它很有用)

我在使用之前就有用户protobuf-net 为此; 这使用谷歌的密集二进制“协议缓冲区”线路格式,其中不包括线路上的任何名称 - 只是数字。 这意味着:

  • 名称不会在线上公开
  • 运行时类中的名称是“DateOfBirth”还是“a1v”并不重要

这通过为成员(属性或字段)分配整数标记来工作 - 例如:

[ProtoMember(4)]
public DateTime DateOfBirth {get;set;}

序列化中仅使用“4”,但(重要的是)您不需要维护大量序列化代码。 如果您需要使用远程处理,也可以非常轻松地连接到ISerialized,如果需要,还可以使用 WCF 钩子。

披露:我是作者,所以我有偏见 - 但该工具是免费的,所以你不会因为查看而失去任何东西;-p

(edit: I decided to write this up as a blog entry, as I've seen it be useful a number of times)

I've had users before use protobuf-net for this; this uses Google's dense binary "protocol buffers" wire format, which doesn't include any names on the wire - just numbers. This means that:

  • the names are not exposed on the wire
  • it doesn't matter whether the name in the class at runtime is "DateOfBirth" or "a1v"

This works by assigning an integer tag to members (properties or fields) - for example:

[ProtoMember(4)]
public DateTime DateOfBirth {get;set;}

Only the "4" is used in the serialization, but (importantly) you don't need to maintain lots of serialization code. Can also be hooked into ISerializable very easily if you need to use remoting, and also has a WCF hook if you want.

Disclosure: I'm the author, so I'm biased - but the tool is free, so you don't lose anything by having a look ;-p

别挽留 2024-07-24 00:38:18

如果您使用 .NET4 或 .NET4.5,您可以定义自己的 DataContractResolver 来重命名属性,但在 [DataMember(...)] 中使用压缩命名会更简单属性

If you're using .NET4 or .NET4.5 you can define your own DataContractResolver to rename the attributes, but it would be simpler to just use compacted named in the [DataMember(...)] attribute

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文