Glass Mapper:查询SitecoreContext时会忽略InferType

时间:2014-01-06 09:31:15

标签: 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; }
}

我有这个项目:

enter image description here

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

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

根据documentation on inferred types,我希望serviceConfig的类型为JavascriptServiceConfiguration,但类型为ServiceConfiguration。我错过了什么吗?我没有对玻璃进行一些具体的配置。

1 个答案:

答案 0 :(得分:14)

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

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

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

        return new IConfigurationLoader[] {attributes };
    }

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