Andriy Svyryd Andriy Svyryd - 1 month ago 7
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

Since there doesn't appear to be an existing library that can accomplish this I wrote one that should handle the simple cases: https://github.com/AndriySvyryd/CSharpScriptSerializer

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

Comments