C#C
C#8mo ago
Lisa

✅ Nested Record Inheritance

I'm trying a bit of inheritance shenanigans, the API I'm working with wraps everything in a data object, which usually has two fields that always appear:

public abstract record CharacterActionResult([property: JsonPropertyName("data")] CharacterActionResultData Data);

public abstract record CharacterActionResultData(
    [property: JsonPropertyName("cooldown")] Cooldown Cooldown,
    [property: JsonPropertyName("character")] Character Character
);


This way I can keep a base record for when I need to access cooldown/character in a generic method, but I can still the define the fields that are different:
public record FightResult(FightResultData Data) : CharacterActionResult(Data);

public record FightResultData(
    [property: JsonPropertyName("fight")] Fight Fight,
    Cooldown Cooldown,
    Character Character
) : CharacterActionResultData(Cooldown, Character);


However, I get an error:
0>FightResult.cs(6,43): Error CS8866 : Record member 'Artifacts.Model.Characters.CharacterActionResult.Data' must be a readable instance property or field of type 'Artifacts.Model.Characters.FightResultData' to match positional parameter 'Data'.
Am I looking over something?
Was this page helpful?