Sitecore、GlassMapper、接口和动态代理

Sitecore, GlassMapper, Interfaces and Dynamic Proxies

在 Sitecore 中,我有 2 个 classes 表示 2 个可能的节点,它们可以作为另一个节点的 children 存在。 我的模型包含一个 Children 属性,它是一个 IEnumerable(INode 由两个 classes 实现)。在运行时,我获取每个 child 的基础类型来识别它们并做一些事情。 在我的测试中,一切都像魅力一样。但实际的实现并没有。

基本上是在剃须刀视图中(继承自 GlassView,我循环遍历 INode children 的集合并调用 .GetType() 来识别底层 class。但不是得到正确的类型,我得到 IColumnProxy 类型,它是动态代理玻璃映射器用来创建视图模型的。

有没有办法获取实际类型而不是动态代理? 谢谢

编辑:这里是代码示例:

public interface INode {}

[SitecoreType(TemplateId = "AAAAAAAAA", AutoMap = true)]
public Class NodeType1 : INode
{
   public string PropertyA { get; set; }
}

[SitecoreType(TemplateId = "BBBBBBB", AutoMap = true)]
public Class NodeType2 : INode
{
   public string PropertyB { get; set; }
}

[SitecoreType(TemplateId = "CCCCCCC", AutoMap = true)]
public class SitecoreItem 
{
   [SitecoreChildren(InferType=true)]
   public virtual IEnumerable<INode> Nodes { get; set; }
}

在剃刀视图中:

@foreach(var node in item.Nodes)
{
  var type = node.GetType(); //INodeProxy
  var isNode1 = node is NodeType1; //False
  var isNode2 = node is NodeType2; //False
  var baseType = node.GetType().BaseType; //DynamicProxy
}

项目中的引用是 Glass.Mapper 和 Glass.Mapper.Sc

编辑:更新 1

好吧,在深入研究之后,GlassMapper 似乎按照我应该的方式进行操作,但出于某种原因,它在我的特定情况下不起作用。 我错过了什么?参考资料?

好吧,经过几天对这个问题的深入研究,我可以肯定的答案是:不是这种情况。基本上,它应该工作。但就我而言,没有办法获得接口的实际实现。 GlassMapper 将实际的 类 替换为 "children" 属性 中声明的接口的动态代理实现,因此无法将每个子项转换为它们的实际类型。

这太糟糕了,因为它阻止了一大堆动态场景的实现。我创建了一个自定义映射器来处理这个问题。但正如我之前所写,它仅在这种特定情况下不起作用。通常它应该可以工作。