C# Possible to make generic functions in base class aware of the type of the calling derived class?

Solution for C# Possible to make generic functions in base class aware of the type of the calling derived class?
is Given Below:

Newbie for OOP here. I want to make different config classes share the same load and save functions:

    public abstract class AbstractConfig
    {
        [JsonIgnore]
        public string FileName { get; private set; }

        public void Save()
        {
            File.WriteAllText(FileName, JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings() { ContractResolver = new SubstituteNullWithEmptyStringContractResolver() }));
        }

        public static T Load<T>(string fileName) where T : AbstractConfig
        {
            T instance = JsonConvert.DeserializeObject<T>(File.ReadAllText(fileName), new JsonSerializerSettings() { ContractResolver = new SubstituteNullWithEmptyStringContractResolver() });
            instance.FileName = fileName;
            return instance;
        }

        public AbstractConfig(string fileName)
        {
            this.FileName = fileName;
        }
    }

    public class ConfigA : AbstractConfig
    {
        [JsonProperty("test1")]
        public string Test1 { get; set; }

        [JsonProperty("test2")]
        public int Test2 { get; set; }

        public static ConfigA Instance { get; set; }

        public ConfigA(string fileName) : base(fileName)
        {
        }
    }

    public class ConfigB: AbstractConfig
    {
        [JsonProperty("test3")]
        public int Test3 { get; set; }

        [JsonProperty("test4")]
        public string Test4 { get; set; }

        public static ConfigB Instance { get; set; }

        public ConfigB(string fileName) : base(fileName)
        {
        }
    }

And I can save like this:

ConfigA configA = new ConfigA("a.json");
// set values for config A
configA.Save();

ConfigB configB = new ConfigB("b.json");
// set values for config B
configB.Save();

But for loading I need to do this:

ConfigA configA = AbstractConfig.Load<ConfigA>("a.json");
ConfigB configB = AbstractConfig.Load<ConfigB>("b.json");

I wonder if it’s possible to make it look like this without the need of overriding the load function in each derived class:

ConfigA configA = ConfigA.Load("a.json");
ConfigB configB = ConfigB.Load("b.json");

Also I wonder if it’s possible to put the singleton Instance and the constructor into the base class (will still be using generics I guess?) so I won’t need to write them for each derived class. Thanks.

Create the abstract class as generic itself –

public abstract class AbstractConfig<T>
{
    [JsonIgnore]
    public string FileName { get; private set; }

    public void Save()
    {
        File.WriteAllText(FileName, JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings()));  
    }

    public static T Load(string fileName)
    {
        dynamic instance = JsonConvert.DeserializeObject<T>(File.ReadAllText(fileName), new JsonSerializerSettings());
        instance.FileName = fileName;
        return (T)instance;
    }

    public AbstractConfig(string fileName)
    {
        FileName = fileName;
    }
}

public class ConfigA : AbstractConfig<ConfigA>
{
    [JsonProperty("test1")]
    public string Test1 { get; set; }

    [JsonProperty("test2")]
    public int Test2 { get; set; }

    public static ConfigA Instance { get; set; }

    public ConfigA(string fileName) : base(fileName)
    {
    }
}

public class ConfigB : AbstractConfig<ConfigB>
{
    [JsonProperty("test3")]
    public int Test3 { get; set; }

    [JsonProperty("test4")]
    public string Test4 { get; set; }

    public static ConfigB Instance { get; set; }

    public ConfigB(string fileName) : base(fileName)
    {
    }
}

Usage –

ConfigA configA = ConfigA.Load("a.json");
ConfigB configB = ConfigB.Load("b.json");