为什么所有 Glass.Mapper 字段都应该是虚拟的?

Why should all Glass.Mapper fields be virtual?

Glass.Mapper documentation states:

using Glass.Mapper.Sc.Configuration.Attributes;

namespace Glass.Mapper.Sites.Sc.Models.Landing
{
    public class HomePage
    {
        public virtual string Title { get; set; }
        public virtual string MainBody { get; set; }
    }
}

You can see that this class doesn't include any special markup however one important thing to remember when writing class with Glass.Mapper is to ensure that all your fields are marked as virtual. Glass.Mapper can automatically infer what data to load for your model by using a combination of On Demand mapping and Auto-Mapping.

它避免使用 "On-demand" 和 "Auto-Mapping" 但没有解释如何或为什么。

有谁知道为什么将这些字段标记为 virtual 很重要?没有这个似乎工作得很好。我看不到的任何东西都继承了字段和 overrides 它们?

如果您使用的是延迟加载,Glass 映射器需要能够即时动态生成一个代理 class 来包装您的 class。为了能够继承和覆盖您的属性,需要将它们标记为 virtual.