Glass Mapper:查询SitecoreContext时会忽略InferType

Kev*_*ühl 10 c# sitecore glass-mapper sitecore7.1

我已经在我的Sitecore 7.1解决方案中安装了软件包Glass.Mapper.Sc.CastleWindsor,3.1.2.11并尝试使用推断类型.我有以下课程:

[SitecoreType]
public class ServiceConfiguration
{
    [SitecoreField(FieldName = "Service Id")]
    public virtual string ServiceId { get; set; }
}

[SitecoreType(TemplateId = "{26512C19-8D30-4A1E-A2CD-3BA89AF70E71}")]
public class JavascriptServiceConfiguration : ServiceConfiguration
{
    [SitecoreField(FieldName = "Is Header Responsive")]
    public virtual bool IsHeaderResponsive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有这个项目:

在此输入图像描述

在我的代码中,我尝试使用以下代码行从glass映射的当前上下文中获取此项:

var serviceConfig = (new SitecoreContext()).GetItem<ServiceConfiguration>("{5436EEC6-1A4D-455F-8EF7-975C51FAE649}", inferType: true);
Run Code Online (Sandbox Code Playgroud)

根据推断类型文档,我希望它是serviceConfig类型JavascriptServiceConfiguration,但它是类型ServiceConfiguration.我错过了什么吗?我没有对玻璃做一些具体的配置.

Mic*_*rds 14

在推断类型之前,必须由Glass.Mapper加载它们.最新版本的Glass会在请求时加载类型,但这不适用于推断类型.要解决此问题,您可以强制Glass在应用程序启动时加载类型.

首先在解决方案中找到GlassMapperScCustom类.然后,您应该更新GlassLoaders方法:

    public static IConfigurationLoader[] GlassLoaders()
    {
        var attributes = new AttributeConfigurationLoader("Your assembly name");

        return new IConfigurationLoader[] {attributes };
    }
Run Code Online (Sandbox Code Playgroud)

如果这不能解决问题,请告诉我.

  • @MichaelEdwards谢谢.您应该更新[教程1](http://glass.lu/docs/tutorial/sitecore/tutorial01/tutorial01.html)和[20](http://glass.lu/docs/tutorial/sitecore /tutorial20/tutorial20.html)也在Glass网站上. (2认同)