Skip to content
This repository was archived by the owner on Oct 19, 2020. It is now read-only.
This repository was archived by the owner on Oct 19, 2020. It is now read-only.

How to convert a dictionary whose value is an interface type? #90

Description

@uheee

Hello, I have a interface and some derived classes, like:

public interface ISomeInterface
{
    int SomeInteger { get; set; }
    string SomeString { get; set; }
}

public class ClassA : ISomeInterface
{
    public int SomeInteger { get; set; } = 1;
    public string SomeString { get; set; } = "This is Class A.";
    public string ClassAProperty { get; set; } = "Only for Class A!";
}

public class ClassB : ISomeInterface
{
    public int SomeInteger { get; set; } = 2;
    public string SomeString { get; set; } = "This is Class B.";
    public string ClassBProperty { get; set; } = "Only for Class B!";
}

And I have a entity class with a IDictionary property with the interface above as value type:

public class TomlEntity
{
    public IDictionary<string, ISomeInterface> Dictionary { get; set; } =
        new Dictionary<string, ISomeInterface>();
}

Now I can write the entity to string but cannot read into it:

var entity = new TomlEntity();
var classA = new ClassA();
var classB = new ClassB();
entity.Dictionary.Add(nameof(ClassA), classA);
entity.Dictionary.Add(nameof(ClassB), classB);
var result = Toml.WriteString(entity);
Console.Write(result);
var reader = Toml.ReadString<TomlEntity>(result); // error

In fact I will have more derived classes and I have their own reflection types, how can I specify their type and instantiate them into the dictionary?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions