2014-01-06 44 views
10

我在我的Sitecore 7.1解決方案中安裝了版本3.1.2.11中的包Glass.Mapper.Sc.CastleWindsor,並試圖使用推斷類型。我有以下類:玻璃映射器:查詢SitecoreContext時會忽略InferType

[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

在我的代碼,我試圖從玻璃下面一行的映射目前情況下獲得這個項目代碼:

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

按照documentation on inferred types,我預計serviceConfig將類型0,但是它的類型是ServiceConfiguration。我錯過了什麼嗎?我沒有對玻璃做特別的配置。

回答

14

在推斷出類型之前,它們必須由Glass.Mapper加載。最新版本的Glass在請求時加載類型,但這對於推斷類型不適用。要解決此問題,可以強制Glass在應用程序啓動時加載類型。

首先在您的解決方案中找到GlassMapperScCustom類。您應該更新GlassLoaders方法:

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

     return new IConfigurationLoader[] {attributes }; 
    } 

讓我知道如果這不能解決它。

+0

這修正了它。我在您的解決方案中將「SitecoreAttributeConfigurationLoader」更改爲「AttributeConfigurationLoader」,因爲Sitecore標記爲過時;-) –

+2

@MichaelEdwards謝謝。您應該可以考慮更新[教程1](http://glass.lu/docs/tutorial/sitecore/tutorial01/tutorial01.html)&[20](http://glass.lu/docs/tutorial/sitecore Glass網站上的/tutorial20/tutorial20.html)。 – GFoley83

相關問題