Given
public record SimpleRec(string PropertyName);
[TestMethod]
public void Test()
{
var simpleRecord = new SimpleRec("property");
var recordString = simpleRecord.ToString();
Console.WriteLine(recordString);
}
Output:
SimpleRec { PropertyName = property }
Is there an equally simple way to instantiate a SimpleRec from recordString?
I was hoping we wouldn't have to mess with XML or JSON serializers.
What puzzles me is that they chose the above string format for the conversion to string in the first place. If it had output Xml, JSON or even YAML, reversing the process would be fairly simple.
I think I want a generic fix for this. Maybe a conversion extension method, but then I need to figure out how to select on record types only. That or just use JSON serialize/deserialize and ignore the default baked-in ToString().
I tried this:
var converted = (SimpleRec)Convert.ChangeType(recordString, typeof(SimpleRec));
But threw an InvalidCastException.