IFormatter 可以反序列化未引用类型的对象吗?

发布于 2024-08-14 11:20:06 字数 156 浏览 3 评论 0原文

假设我使用 BinaryFormatter 序列化了第三方库类型对象。不引用此库的程序集会尝试反序列化字节。它会起作用吗?

我不希望它被转换为正确的类型,我只想将它作为 object 实例检索,以便我可以对其进行分组并再次序列化。

Supose I serialized a third party library type object with BinaryFormatter. An assemby that does not references this library tries to deserialize the bytes. Will it work?

I do not expect to it be cast to the correct type, I just want to retrieve it as an object instance so I can group it and serialize it again.

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

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

发布评论

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

评论(5

貪欢 2024-08-21 11:20:06

这应该可以正常工作,但前提是该库与应用程序一起部署。您不需要直接引用它。

在序列化期间,BinaryFormatter 存储完整的程序集名称(显示名称、公钥令牌、版本号)以及类型信息。这些信息足以让解串器启动并再次加载该程序集。请注意,不会存储路径信息,因此需要在执行反序列化的应用程序中部署程序集。

This should work fine, but only if that library is deployed with the app. You don't need a direct reference to it.

During serialization, the BinaryFormatter stores the full assembly name (display name, public key token, version number) along with the type information. This is enough information for the deserializer to go off and load that assembly again. Note that path information is not stored, hence the need to deploy the assembly in the app that does the deserialization.

请止步禁区 2024-08-21 11:20:06

不;如果类型本身未被引用(以某种方式被某些加载的程序集引用),则无法实例化它。即使您不需要(或想要)以强类型方式引用实例,对象本身也必须仍然是该类型的实例。

如果程序集可用(并且可发现),那么它将被加载,但从严格意义上来说,您将无法从完全未引用的程序集反序列化类型。

No; if the type itself isn't referenced (in some way by some loaded assembly), then it can't be instantiated. Even if you don't need (or want) to reference the instance in a strongly-typed way, the object itself must still be an instance of that type.

If the assembly is available (and discoverable), then it will be loaded, but in a strict sense no, you won't be able to deserialize a type from a completely unreferenced assembly.

古镇旧梦 2024-08-21 11:20:06

如果您只是想暂时获取序列化信息以便对其进行分组,您可以只从序列化流中读取原始字节并对它们进行分组吗?也许进入 List 实例?这假设最终目的地可以对每个字节数组表示的信息做出一些假设。

If you are only trying to temporarily obtain the serialized information so that you can group it, can you just read the raw bytes from the serialized stream and group them? Maybe into a List<byte[]> instance? This assumes that the final destination can make some assumptions about the information represented by each byte array.

带上头具痛哭 2024-08-21 11:20:06

不,它不起作用,为了反序列化对象,您需要引用定义该对象的程序集。

No, it won't work, in order to deserialize a object you need to reference the assembly where the object is defined.

阳光的暖冬 2024-08-21 11:20:06

是的,如果您创建序列化绑定器,则可以将该类型反序列化为不同的类型。但是如果没有定义,您将无法实例化原始类型的实例(您需要手头有程序集)

Yes, if you create a serialization binder, you can deserialize the type to a different type. But you won't be able to instantiate an instance of the original type without it's definition (to which you'd be required to have the assembly on hand)

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