Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a serializer for .net which will output c# code?

I'm looking for a serializer which could take an instance, and serialize it to a string which would contain c# code, representing the contents of the graph. The class would function similar to SerializeObject in JSON.NET.

I know only a very narrow set of structures will work, but the ones I'm interested in are quite simple and they would.

Bonus points if anyone knows of a Visual Studio Visualizer with similar functionality.

Edit: The output will be used in a different application at compile time. I don't need to deserialize the output(c# code) at runtime, it gets saved to a file for analysis.

var foo = new Foo() { Number = 1, Bar = new Bar() { Str = "Bar"}};
string sourceCode = Magic.SerializeObject(foo);

Output:

Foo obj = new Foo();
obj.Number = 1;
obj.RefType = null; // infer this
obj.Bar = new Bar();
obj.Bar.Str = "Bar";
like image 677
jasper Avatar asked Apr 16 '13 23:04

jasper


People also ask

What is serialization C#?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

How many types of serialization are there in C#?

Types of Serialization Serialization can be of the following types: 1. Binary Serialization 2. SOAP Serialization 3. XML Serialization Binary Serialization: Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically.

What is serialization in .NET core?

Serialization is the process of converting the state of an object into a form (string, byte array, or stream) that can be persisted or transported. Deserialization is the process of converting the serialized stream of data into the original object state.


1 Answers

yes, and no...

The closest solution is called CodeDOM, it's what most Visual Studio designers and wizards use to generate code.


After taking a closer look at your comments I think you should read thoroughly the Binary Serialization section in the MSDN, It will fulfill all of your requirements.

What you're looking for is serialization, you don't want to recompile the second project every time right? you just want validation , and forward \ backward compatibility .

like image 56
AK_ Avatar answered Nov 13 '22 13:11

AK_