YamlDotNet找不到属性 [英] YamlDotNet can not find property
问题描述
我正在尝试使用 YamlDotNet .需要注意的是,我希望域模型是只读的,因此我试图通过继承和内部设置器解决这个问题.
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:
在类型上找不到属性'HtmlTemplate'"ConsoleApplication1.Repositories.YamlTemplateRepository + DeserializeableTemplate".
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?
调用YamlDotNet的代码如下:
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...
}
以及我要继承的类:
public class Template
{
public string HtmlTemplate { get; internal set; }
// A few more properties...
}
(具有以下功能的小型控制台测试应用程序可以在这里找到相同的错误)
推荐答案
老问题,但是我遇到了类似的问题,可以通过将继承的属性设置器的访问修饰符更改为受保护的来解决.我猜这里使用的 internal
修饰符在反序列化方面发挥了作用.对于有关使模型真正变为只读的问题,这可能是不需要的解决方案,但我想与以后的疑难解答者分享我的解决方案.
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.
这篇关于YamlDotNet找不到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!