1

I have an object which

  • does not have the Serializable attribute set
  • Has properties, who's type, does not have the Serializable attribute set
  • I do not have control over (meaning i cannot edit the class)

I tried reading THIS, it talks about substitution classes to fix this when using Sharpserializer but frankly, I don't understand how to do this when I don't know the properties of my object.

are there some of serialization frameworks that can do this?

Edit: I'm looking into protobuf.net I cannot figure out how to get it to work in my scenario though - Im hoping Marc will swing by to save the day? :) - I read this which is the exact same problem as mine, but I'm still getting "Type is not expected and no contract can be inferred" when using

private static byte[] ClienToBytes(IScsClient client)
{
    using (var memoryStream = new MemoryStream())
    {
        RuntimeTypeModel.Default.Add(typeof(IScsClient), true).SetSurrogate(typeof(BinaryFormatterSurrogate<IScsClient>));

        Serializer.Serialize(memoryStream, client);
        return memoryStream.ToArray();
    }
}

am I using the RunTimeTypeModel wrong?

Community
  • 1
  • 1
VisualBean
  • 4,908
  • 2
  • 28
  • 57
  • Is `IScsClient` an interface? or a class? Or better: do you have a *runnable example* (even if it doesn't actually work) of what you want to do? – Marc Gravell Aug 30 '13 at 13:31
  • Yay! - I think IScsClient is a class (bad naming maybe?) - I can create an object from it(can i do this with an interface?), that has properties like IscsWireProtocol WireProtocol - which is what i want to serialize and deserialize because it has some connection properties i need later on. I do have a runnable Test of what i want to do – VisualBean Aug 30 '13 at 13:39
  • and is the concrete implementation "obvious" (for some value of "obvious")? Or even better : can you share the declaration of `IScsClient` and any concrete implementation? (by email would help if you can't put it here for some reason) – Marc Gravell Aug 30 '13 at 13:56
  • IScsClient Client = ScsClientFactory.CreateClient(new ScsTcpEndPoint("160.209.1.100",10666)); this is about as concrete as it gets Source for IscsClient is -> http://www.codeproject.com/Articles/155282/A-Complete-TCP-Server-Client-Communication-and-RMI – VisualBean Aug 30 '13 at 14:02
  • Well, sort of; I kinda need to know a: what the interesting properties of `IScsClient` are, and b: whether they are set via the instance (accessors) or via the `CreateClient` line. – Marc Gravell Aug 30 '13 at 14:04
  • the property types are non native (well there are some like datetime) but the rest is a part of the framework im using - and they are set via CreateClient and when i connect the using Client.Connect() it uses some internal logic to setup a socket which in turn gives me communicationState amongst other things – VisualBean Aug 30 '13 at 14:07
  • And is there enough information on an `IScsClient` to know how to create a new instance? i.e. if you had to clone one right now, and all you had was an `IScsClient` - could you do something like `var clone = ScsClientFactory.Create(source.Host, source.Port, source.Magic, source.Foo, source.Bar)` or something? – Marc Gravell Aug 30 '13 at 14:08
  • Basically - it is really hard to answer without the exact context; the answer could be "yes, like this", or "no, you need to rethink" - but I can't tell much without knowing the API involved – Marc Gravell Aug 30 '13 at 14:09
  • No, that is in fact my problem, else i would have create my own object to hold the values. I cannot recreate the object from the properties. I've listed the source in my third comment - btw. thank you so much for putting so much effort into this – VisualBean Aug 30 '13 at 14:10
  • 1
    Then: neither can protobuf-net (at least, not in any sane way); it isn't an internal state serializer. I would suggest thinking about what the **data is** that you need to configure the system; serialize just the data, and then *separately* map that to implementation. At the moment, my answer would be "no, this is not a good fit" – Marc Gravell Aug 30 '13 at 14:11
  • I should emphasise: between custom factory methods and type surrogates (both fully supported), many scenarios involving custom constructors are fine - just... Not really this one – Marc Gravell Aug 31 '13 at 00:05

1 Answers1

0

I would try protobuf-net. Take a look here: http://code.google.com/p/protobuf-net/

Quote from Website:

protocol buffers is the name of the binary serialization format used by Google for much of their data communications. It is designed to be:

  • small in size - efficient data storage (far smaller than xml)
  • cheap to process - both at the client and server
  • platform independent - portable between different programming architectures
  • extensible - to add new data to old messages
Community
  • 1
  • 1
S.Spieker
  • 7,005
  • 8
  • 44
  • 50
  • I've looked into this too, but doesn't seem be any easier to use non custom, non native classes – VisualBean Aug 30 '13 at 11:10
  • But you can Add Serializer for typoes you do not have the sourcecode in your hand, for example: `RuntimeTypeModel.Default.Add(typeof(Point), false).Add("X", "Y");` – S.Spieker Aug 30 '13 at 11:12
  • but i cannot mark the type as a contract? - do i not need to do this? – VisualBean Aug 30 '13 at 11:17
  • You do not have to modify the classes. The class Point is from Namespace System.Drawing.Point and there is no serializer defined for that out of the box. – S.Spieker Aug 30 '13 at 11:23
  • Do you have any examples of using the RuntimeTypeModel? – VisualBean Aug 30 '13 at 11:30
  • the above sample tells the serializer which properties should be serialized. For the struct Point i'm only interested in the Property X and Y and the rest i don't care, although there are others. More examples at: [link]http://stackoverflow.com/questions/6088004/how-runtimetypemodel-can-be-used-to-associate-protoinclude-with-a-type-in-protob – S.Spieker Aug 30 '13 at 11:35