gpt4 book ai didi

c# - Glass Mapper v4 InferType Cast 问题

转载 作者:太空狗 更新时间:2023-10-29 22:58:07 25 4
gpt4 key购买 nike

我不明白为什么 Glass Mapper 不能像描述的那样以正确的方式转换对象 here我有下一节课

 public class BC
{
[SitecoreId]
public virtual ID Id { get; set; }
}

public class WB : BC
{
[SitecoreField(FieldName = "P1")]
public virtual Glass.Mapper.Sc.Fields.Link P1 { get; set; }
}

[SitecoreType(TemplateId = "{XXX}", AutoMap = true, EnforceTemplate = SitecoreEnforceTemplate.Template)]
public class AAA : WB
{
public virtual string AAAP1 { get; set; }
public virtual DateTime AAAP2 { get; set; }
}
[SitecoreType(TemplateId = "{VVV}", AutoMap = true, EnforceTemplate = SitecoreEnforceTemplate.Template)]
public class BBB : WB
{
public virtual string BBBp1 { get; set; }
public virtual DateTime BBBp2 { get; set; }
}

[SitecoreType(TemplateId = "{YYY}", AutoMap = true)]
public class RazorRenderClass: BC
{
[SitecoreChildren(InferType = true)]
public virtual IEnumerable<WB> Children { get; set; }

[SitecoreChildren(InferType = true)]
public virtual IEnumerable<AAA> AAACh { get; set; }

[SitecoreChildren(InferType = true)]
public virtual IEnumerable<BBB> BBBCh { get; set; }
}

在 Razor View 中,如果我使用 Children 属性,我无法获得 AAA 或 BBB 对象,

 @foreach (var child in Model.Children)
{
if (child is BBB)
{
var news = child as BBB;
<li>
11
</li>
}
else if (child is AAA)
{
var evt = child as AAA;
<li>
222
</li>
}
}

更有趣的是,如果我在 cshtml 中调用 BBBCh 或 AAACh 属性,我可以看到(在调试时)Children 属性包含正确的项目(对象),但是如果我尝试从 Children 属性中获取任何项目,例如

  var detailWidget = Model.Children.FirstOrDefault();

它将转换为 WB 类。我可以用它做什么?

最佳答案

我遇到了完全相同的问题,版本 4.0.5.54。

我的解决方案是将子类型指定为属性,就像您对 AAACh 和 BBBch 所做的那样。但是设置时不延迟加载它们。

[SitecoreType(TemplateId = "{YYY}", AutoMap = true)]
public class RazorRenderClass: BC
{
[SitecoreChildren(InferType = true, IsLazy = false)]
public virtual IEnumerable<WB> Children { get; set; }

[SitecoreChildren(InferType = true, IsLazy = false)]
public virtual IEnumerable<AAA> AAACh { get; set; }

[SitecoreChildren(InferType = true, IsLazy = false)]
public virtual IEnumerable<BBB> BBBCh { get; set; }
}

我真的不喜欢必须指定 AAACh 和 BBBch 作为属性。在 glassmapper v3 中,以下内容就足够了。

[SitecoreType(TemplateId = "{YYY}", AutoMap = true)]
public class RazorRenderClass: BC
{
[SitecoreChildren(InferType = true)]
public virtual IEnumerable<WB> Children { get; set; }
}

关于c# - Glass Mapper v4 InferType Cast 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32638505/

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