views:

142

answers:

2

Is there a library or code sample for converting an in memory POCO c# object to a .cs code file that creates that object. An example: object of type car in memory becomes:

Car c = new Car 
{ 
     Name = "mazda", 
     Id = 5,
     Passengers = new List<string> { "Bob", "Sally" }
     // etc... recursing to the bottom
};

I could assume it could only set public properties.

A: 

We've used binary serialization to serialize objects to file, but nothing to readable .CS files.

Mark
A: 

You could use the Visual Studio T4 code templates to gen your .cs files. You can check out the info on the code generation tools in this article and see if its what your looking for.

You would need to setup a partial code template and have your library function write out the rest of the code template, then run the templates to generate your .cs class that creates your POCO objects. Not sure if this is the only way to do it but I could see it working.

Tj Kellie