1

I need to generate or define new class based on deserialization serialized class. So I want to transfer class definition from server to client to have access to it's properties later.

Is it possible and how?

cnd
  • 32,616
  • 62
  • 183
  • 313

2 Answers2

1

Proper way to do it would be to either expose a schema definition for your service for clients to consume & generate strongly type class definitions from that or provide a DLL with your DTO contract definitions (class/interface definitions) to the client.

If you chose neither of those approaches (no schema & no dll with interfaces) but still want to generate a class definition, you can in an improper way generate .cs class definitions, from a sample data of the service (call the services couple of times and intercept the responses or use some http client). However this approach does not guarantee that you will get an accurate or/and complete generation. Basically you can go from:

XML->XSD->C# cs class file (or even XML to C# cs file directly)or JSON->C# class file

And deserializing object to dynamic especially when you don't own both the server & client code is pretty much the worst thing you can do. And this way you didn't transfer you class definition to the client. Deserializng to dynamic objects is actually no desrialization at all as matter of fact, it gives you a dictionary of strings with syntactical sugar to access them as properties at runtime with not compile time support which can be equal to a disaster. In short don't do it unless you own all the code (not that it's a good idea then either but maybe you could get by somehow)

user1416420
  • 498
  • 2
  • 7
0

One portable way to transfer the property definitions and the data itself is to use the JSON serializer.

You can deserialize into a dynamic object using JSON.Net

Deserialize json object into dynamic object using Json.net

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 1
    For WCF this is likely the worst idea -> on the other hand DTO contracts dll is likely the best & schema definitions is somewhere in the middle – user1416420 Feb 12 '13 at 07:25
  • @user1416420: Sure, I would use a contract pre-understood by both parties. However, my interpretation of the question is how to transport the schema over the wire as well as the data. JSON does that well. Of course, a web service can also publish its schema using XML. Without knowing the requirement behind the request, it's hard to say with certainty what the "proper" way is. – Eric J. Feb 12 '13 at 17:39