lambda表达式,封闭过的变量,显示类,串行化和prevalence层表达式、变量、串行化、lambda

2023-09-03 22:48:54 作者:又酷又能打.

我已经实现了一个prevalence层精简框架(包括的BinaryFormatter 般串行)。我想能够序列化而导致的诸如lambda表达式和迭代器在适当情况下,编译器生成的类,因此,如果(比如)一个lambda和闭于变量(即,在显示类的实例)被添加到一个事件上的序列化对象,并且所有的封闭过变量序列化,那么得到的对象图还是完全序列化。

I've implemented a prevalence layer for Compact Framework (including a BinaryFormatter-like serializer). I'd like to be able to serialize the compiler-generated classes that result from such things as lambdas and iterators where appropriate, so that if (for example) a lambda and its closed-over variables (that is, the display class instance) is added to an event on a serializable object, and all the closed-over variables are serializable, then the resulting object graph is still fully serializable.

这是可以接受的,如果这些类的实例只能通过在完全一样的构建,他们从序列化的二进制代码进行反序列化 - 在prevalence层主要用于提供耐用性,如申请被意外终止(停电,设备重启,是不同的可能性),以及串行数据流预计不会向前或向后兼容的,甚至为此事跨越同一来源$ C ​​$ C两个编译兼容 - 一切结果将被发送到一个服务器时,我们接下来谈谈也无妨,我们不打算更新,同时断开连接。

It's acceptable if instances of these classes can only be deserialized by the exact same build of the binaries that they were serialized from -- the prevalence layer is mostly to provide durability should the application be terminated unexpectedly (power failure and device reboot are distinct possibilities), and the serialized data stream is not expected to be either forward or backward compatible, or even for that matter compatible across two compiles of the same source code -- everything of consequence will be sent up to a server when we next talk to it anyway, and we're not going to update while disconnected.

难道是我的格式化处理这些编译器生成的类,好像他们是序列化这个有限的范围内合理吗?我看到的唯一选择是手工执行什么areotherwise编译器支持的图案无处不在串行化是一个问题,其后果从过于冗长到几乎无法读取。

Is it reasonable in this limited context for my formatter to treat these compiler-generated classes as if they were serializable? The only alternative I see is to hand-implement what areotherwise compiler-supported patterns everywhere serializability is a concern, with consequences ranging from the overly verbose to the nearly unreadable.

推荐答案

我看你做了什么的,但在我的经验,这是一个更好的主意专心序列化的数据的,并处理通过回滚/前进到已知状态,可能使用类似本地CQRS队列,或存放你如何到达那里的其他方式的耐用性。

I see what you are getting at, but in my experience it is a much better idea to concentrate on serializing data, and handle the durability by rolling back/forward to a known state, perhaps using something like a local CQRS queue, or some other way of storing "how you got there".

回复的具体问题,问题的紧范围内(仅工作在相同的生成等),我的猜到的应该是美好的,但它取决于很多是否任何的是的这些变量捕获的有没有合理的序列化的 - 也就是像UI元素(容易意外捕获一个无形的这个),该不能被重构(os的手柄等)。如果是孤立的数据(我指的是:该图是从您自己的code不仅仅是数据 - 不依赖非托管),那么我想它的应的是确定

Re the specific question, within the tight scope of the question (only working on the same build etc) I guess that should be OK, but it depends a lot on whether anything that is captured in those variables has no sensible serialization - i.e. something like a UI element (easily accidentally captured with an invisible this) that cannot be reconstructed (os handles, etc). If it is isolated data (by which I mean: the graph is just data from within your own code - no unmanaged dependencies), then I guess it should be OK.

另一个问题是,CF没有太多的强反射可以在完整的框架,这可能使这更尴尬的一点比它可能会在常规的框架( GetUninitializedObject 为例)。也许是可行的,但更多的工作。

The other issue is that CF lacks much of the stronger reflection available in the full framework, that might make this a bit more awkward than it might be on regular framework (GetUninitializedObject for example). Probably doable, but a bit more work.