Andriy Svyryd Andriy Svyryd - 8 months ago 62
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);


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);