YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Instantiate concrete type from propertyname

Open hwalkenshaw opened this issue 5 years ago • 1 comments

Hi,

I just wanted to see if there is an easier way to tell the yaml deserializer which concrete implementation to use in case a property is either an interface or base class?

I am aware of WithTagMapping that accomplishing this, however I want to see if the following could be possible:

`public interface IType {

}

public class Implementation1
{
    public string MyValue { get; set; }
}

public class Implementation2
{

}

public class MyClass
{
    public IType MyProperty { get; set; }
}`

And then deserializing it with something like the following: `var yaml = @" Implementation1: MyValue: 1 ";

        var deserializer = new DeserializerBuilder()
         .WithPropertyMapping("Implementation1", typeof(Implementation1)) // With propertymapping does not exist, just want to illustrate conceptually what I want to achieve
         .WithPropertyMapping("Implementation2", typeof(Implementation2))
         .Build();
        

        var op = deserializer.Deserialize<MyClass>(yaml);`

Is there a current way to do this? Maybe worth having a look to add this to the library?

hwalkenshaw avatar Jun 04 '19 11:06 hwalkenshaw