Andriy Svyryd Andriy Svyryd - 1 year ago 127
C# Question

Serialize to C# Script

Roslyn scripting allows to evaluate a C# script containing just an object creation expression, effectively allowing to deserialize objects from scripts:

var script = "new Point { X = 1, Y = 2 }";
var point = await CSharpScript.EvaluateAsync<T>(script);

Is there a library that allows to do the reverse serialization?

script = ???.Serialize(point);

Answer Source

Since there doesn't appear to be an existing library that can accomplish this I wrote one that should handle the simple cases:

var input = new Point {X = 1, Y = 1}; var script = CSScriptSerializer.Serialize(input); var output = CSScriptSerializer.Deserialize<Point>(script);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download