The following simple map between record structs (or plain structs) blows up -
public record struct Struct1
{
public int Value { get; set; }
}
public record struct Struct2
{
public int Value { get; set; }
}
[TestFixture]
public class AutomappingTests
{
[Test]
public static void TestMapServiceStack()
{
var x = new Struct1 {Value = 42};
var y = x.ConvertTo<Struct2>(); // blows up
Assert.Pass()
}
Of course, if the type is class (or plain record) this works fine; if it’s a record struct or a struct it blows up.
public struct Struct1
{
public int Value { get; set; }
public Struct1(string value) => Value = int.Parse(value);
public override string ToString() => $"{Value}";
}
public struct Struct2
{
public int Value { get; set; }
public Struct2(string value) => Value = int.Parse(value);
public override string ToString() => $"{Value}";
}
Alternatively if you just want to be able to convert them you can just implement its implicit casting which is more efficient when its implementation exists:
public record struct Struct1
{
public int Value { get; set; }
public static implicit operator Struct1(Struct2 from) =>
new() { Value = from.Value };
}
public record struct Struct2
{
public int Value { get; set; }
public static implicit operator Struct2(Struct1 from) =>
new() { Value = from.Value };
}