在 SecurityTransparent 程序集中序列化不可变结构
我正在 .NET 4 上启动一个新项目。我将创建的所有库都将应用 SecurityTransparentAttribute
。
现在,我在这些程序集中有一个需要序列化的不可变结构。作为一个不可变的对象,任何属性都不会有设置器(例如 System.DateTime
)。
如果我只是将结构标记为 [Serializing]
,则不会序列化任何内容,因为属性是只读的。处理此问题的常用方法是实现 ISerialized
接口,并在 GetObjectData
和特殊构造函数中处理它。
但在这种情况下,我的程序集将是安全透明的,并且 ISerialized.GetObjectData
是 SecurityCritical
,所以我不能这样做。
那么,我有什么选择呢?我真的很想拥有一切SecurityTransparent
,因为我真的不需要任何关键的东西。除了这个。
谢谢
I'm starting a new project on .NET 4. All the libraries I'll be creating will have SecurityTransparentAttribute
applied.
Now I have an immutable struct in one of these assemblies that needs to be serialized. As an immutable object, there won't be setters for any property (like with System.DateTime
).
If I simply mark the struct as [Serializable]
, nothing will be serialized, as the properties are readonly. The usual way to deal with this is to implement the ISerializable
interface and take care of it in GetObjectData
and the special constructor.
But in this case, my assembly will be security transparent, and ISerializable.GetObjectData
is SecurityCritical
, so I can't do it this way.
So, what are my options here? I would really like to have everything SecurityTransparent
because I won't really need any critical stuff. Except this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
BinaryFormatter
,它会序列化结构体的字段,而不是其属性,并且即使字段是只读
,它也能工作。因此,如果适合通过简单地序列化结构体的字段来序列化结构体,那么一切就都准备好了。If you're using
BinaryFormatter
, it serializes the fields of your struct, not its properties, and it works even if the fields arereadonly
. So if it's appropriate to serialize your struct by simply serializing its fields, you're all set.我的第一个想法是,关于如何序列化自身的知识在结构中是否有太多的行为?
您可以沿着为其创建类型转换器的路线
My first thought is that is the knowledge of how to serialize itself too much behaviour to have in a struct?
You could go down the route of creating a type converter for it