Skip to content

Feature request: Method to clone an IniSection #12

@SadPencil

Description

@SadPencil

Situation: save a base section as a template, containing some common k-v pairs, and to allow additional k-v pairs append on it

First, read an IniSection from template.ini file:

[Template]
foo=bar
var templateIni = new IniFile("template.ini");
var templateSection = templateIni.GetSection("Template");

Then, append this section to instance.ini file with a modified section name:

var instanceIni = new IniFile();

// need to clone the IniSection to prevent `templateSection` be modified
instanceSection = templateSection.Clone("new section name"); // or should the section name be settable?
instanceSection.AddKey("foo", "bar");
instanceIni.AddSection(instanceSection);
instanceIni.WriteIniFile("instance.ini");

Due to missing Clone() method, I have to use the following codes instead

IniSection newSection = new("new section name");
foreach (var kvp in oldSection.Keys)
{
    newSection.AddKey(kvp.Key, kvp.Value);
}

But this exposes Keys and more importantly, assumes that only section name and Keys variables are all elements of an IniSection. So this code is not robust against library upgrades.
So should be better to provide a Clone() method or a constructor accepting a section name with an existing IniSection as a parameter?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions