gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:50:14 25 4
gpt4 key购买 nike

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.

它没有使用“按需”和“自动映射”,但没有解释如何使用或为什么使用。

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

最佳答案

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

关于c# - 为什么所有 Glass.Mapper 字段都应该是虚拟的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com