In Sitecore, I Have 2 classes representing 2 possible nodes that can live as children of another node. My model contains a Children property which is an IEnumerable<INode> (INode being implemented by both classes). At runtime, I get the underlying type of each child to identify them and do some stuff. Everything works like a charm in my tests. But the actual implementation doesn't.
Basically in a razor view (inheriting from GlassView, I cycle through the collection of INode children and call .GetType() to identify the underlying class. But instead of getting the correct type, I get IColumnProxy type, which is the dynamic proxy glass mapper uses to create the view model.
Is there a way to get the actual type instead of the dynamic proxy? Thanks
EDIT: here is code example:
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; }
}
And in the razor view:
@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
}
References in projects are Glass.Mapper and Glass.Mapper.Sc
EDIT: Update 1
Well, after digging a bit into it, it seems GlassMapper does it the way I am supposed to, but for some reason it doesn't work in my specific case. What Am I missing? References?
GetType()
did you tryif (node is MyType)
? glass.lu/Mapper/Sc/Tutorials/Tutorial17 – jammykam