问题
I've written a custom serialization routine that does not use ISerializable or the SerialzableAttribute to save my objects to a file.
I also remote these same objects and would like to use the same serialization technique. However, I don't want to implement ISerializable because my serialization method is completely decoupled from my objects (and I'd like for it to stay that way).
Is there an easy way (possibly with remoting sinks) where I can take a stream and write bytes to it and on the other side read bytes from it, skipping the Serialization framework in .NET?
回答1:
If you want to use remoting then you are limited to BinaryFormatter
. Normally, you can use a "serialization surrogate" to provide a serializer separate to the formatter, but AFAIK this deoesn't work with .NET remoting.
However; if you write your own RPC stack (over TCP/IP or HTTP, for example), you'll have a lot more control. Equally, with WCF you can replace the serializer via a behavior. I use both of these tricks in protobuf-net (the WCF hooks are here).
Not sure you can do this with remoting though - you'd probably have to use ISerializable
.
回答2:
If you want to explicitly deal with streaming bytes between your processes, use pipes. Here's one getting-started on named pipes, Google has plenty as well.
回答3:
You can implement IClientFormatterSinkProvider & IServerFormatterSinkProvider and configure channel formatter (f.e. via configuration).
See BinaryServerFormatterSinkProvider / BinaryServerFormatterSink and BinaryClientFormatterSinkProvider / BinaryClientFormatterSink for implementation details.
来源:https://stackoverflow.com/questions/761770/how-to-use-custom-serialization-during-net-remoting