C#C
C#3y ago
SWEETPONY

❔ How to fix "Target runtime doesn't support covariant types in overrides?

I have Builder, let's see code:
public abstract class UISchemaBuilderBase
{
   protected readonly UISchemaSpecification UiSchemaSpecification;

   public UISchemaSpecification Builder() =>
      UiSchemaSpecification;

   public virtual UISchemaBuilderBase AddBoolean(
       string fieldName,
       string groupName = null)

   {
       .. some logic
    
       UiSchemaSpecification.Properties[ fieldName ] = schema;
       return this;
   }
}

public class UISchemaBuilder : UISchemaBuilderBase
{
    public UISchemaBuilder() : base(new UISchemaSpecification()) { }

    public UISchemaGroupBuilder StartGroup(string name)
    {
        UiSchemaSpecification.ParsecMetadata ??= new();
        UiSchemaSpecification.ParsecMetadata.Groups ??= new();
        UiSchemaSpecification.ParsecMetadata.Groups.Add( new(name) );

        return new UISchemaGroupBuilder(name, this, UiSchemaSpecification);
    }
}

public class UISchemaGroupBuilder : UISchemaBuilderBase
{
    private readonly string _name;
    private readonly UISchemaBuilder _schemaBuilder;

    public UISchemaGroupBuilder(
       string name,
       UISchemaBuilder schemaBuilder,
       UISchemaSpecification specification)
       : base(specification)
    {
        _name = name;
        _schemaBuilder = schemaBuilder;
    }

   public override UISchemaGroupBuilder AddBoolean(
       string fieldName,
       string fieldTitle,
       string groupName = null)
    {
        base.AddBoolean(fieldName, fieldTitle, _name);
        return this;
    }
}

So implemented code allows use to do this:

var builder = new UISchemaBuilder()
  .StartGroup("groupName")
    .AddBoolean()
  .EndGroup()
  .StartGroup("anotherGroupName")
    .AddBoolean()
  .EndGroup()
  .Build();
Was this page helpful?