YamlDotNet can not find property

1.8k views Asked by At

I am trying to create a simple model for parsing a yaml file to my domain object using YamlDotNet. The caveat is, that I want the domain model to be readonly, so I'm attempting to solve this through inheritance and internal setters.

For some reason though, the library throws an exception stating:

Property 'HtmlTemplate' not found on type 'ConsoleApplication1.Repositories.YamlTemplateRepository+DeserializeableTemplate'.

I am using an alias, but even scratching that, and using a test class with the right property names does not set it right.

What am I doing wrong? Have I misunderstood how the library should be used?

The code that calls YamlDotNet looks like this:

deserializer.Deserialize<DeserializeableTemplate>(yamlContents);

and the class I'm deserializing looks like this:

private class DeserializeableTemplate : Template
{
  [YamlMember(Alias = "HtmlTemplate")]
  public string HtmlTemplateWrapper
  {
    get { return HtmlTemplate; }
    set { HtmlTemplate = value; }
  }

  // A few more properties...
}

and the class I am inheriting:

public class Template
{
  public string HtmlTemplate { get; internal set; }
  // A few more properties...
}

(Small console test application with the same error can be found here)

1

There are 1 answers

3
Gliffie On

Old question, but I had a similar issue, which was solved by changing the access modifier of the inherited property setter to protected. I'm guessing the internal modifier used here is playing tricks on the deserialization. This might be an unwanted solution for this problem regarding making the model truly readonly, but I wanted to share my solution for future troubleshooters.