I am trying to use Glass.Mapper's InferType property. I've read through the tutorial on the Glass website. I have two alert types that have different markup. They both inherit from the same base class AssetAlert that contains a property that holds the alert message. This code does not work as it is and when I debug it, the type returned is of the base class instead of the appropriate derived class. I have already updated GlassMapperScCustom.cs like the tutorial says.
How can I confirm that Glass has loaded all of the models from my assembly properly?
And if that's not the issue, can anyone see anything wrong with my code that might be preventing it from inferring the correct derived class instead of the base class?
I'm using Glass.Mapper 4.3.4.196 and Sitecore 8.2 rev. 161115
I have these classes:
FundDetail.cs
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
[SitecoreType(AutoMap = true, TemplateId = "{1813C6E1-72AD-41B9-9D68-372A843BCCCB}")]
public class FundDetail : StandardItem
{
[SitecoreQuery("./Alerts/*", IsRelative = true, InferType = true)]
public virtual IEnumerable<AssetAlert> AssetAlerts { get; [UsedImplicitly] set; }
}
AssetAlert.cs
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
[SitecoreType(AutoMap = true, TemplateId = "{607D5F70-D3A5-4690-A2EC-259A4700D2E3}")]
public class AssetAlert : StandardItem
{
[SitecoreField("{0797970F-D9E6-4FB2-86EA-CF271CCA0629}")]
public virtual string Message { get; [UsedImplicitly] set; }
}
AssetUpdateAlert.cs
[SitecoreType(TemplateId = "{9DAE0D23-6524-4D31-B23E-B4D16E1ADDE0}")]
public class AssetUpdateAlert : AssetAlert
{
private const string DictionaryFolder = "/Content Types/Asset Alert/";
public static string Title
=> DictionaryPhraseRepository.Current.Get(DictionaryFolder + "Update Title");
}
AssetShareholderInformationAlert.cs
[SitecoreType(TemplateId = "{2B5FB48B-4ED8-4B3A-8D29-AFDF5A3D66FB}")]
public class AssetShareholderInformationAlert : AssetAlert
{
private const string DictionaryFolder = "/Content Types/Asset Alert/";
public static string Title
=> DictionaryPhraseRepository.Current.Get(DictionaryFolder + "Shareholder Information Title");
}
FundDetail.cshtml
(I removed the markup for readability)
@if (Model.AssetAlerts != null)
{
foreach (var alert in Model.AssetAlerts)
{
@if (alert is AssetUpdateAlert)
{
@AssetUpdateAlert.Title
@Editable(alert, a => a.Message)
}
else if (alert is AssetShareholderInformationAlert)
{
@AssetShareholderInformationAlert.Title
@Editable(alert, a => a.Message)
}
}
}
alert as AssetUpdateAlert