2017-08-24 93 views
0

我已經創建了一個新的配置文件Special.config被設置後立即:Sitefinity config屬性空,甚至

<?xml version="1.0" encoding="utf-8"?> 

<SpecialConfig xmlns:config="urn:telerik:sitefinity:configuration" xmlns:type="urn:telerik:sitefinity:configuration:type" config:version="10.0.6401.0"> 
    <UnicornSettings HornSize="#{HornSize}" HoofColor="#{HoofColor}" /> 
</SpecialConfig> 

然後followed the documentation設置一對類(並註冊在Global.asax的配置。 CS)文件:

public class SpecialConfig : ConfigSection 
{ 
    public UnicornSettingsElement UnicornSettings 
    { 
     get 
     { 
      return (UnicornSettingsElement)this["UnicornSettings"]; 
     } 
     set 
     { 
      this["UnicornSettings"] = value; 
     } 
    } 
} 

public class UnicornSettingsElement : ConfigElement 
{ 
    public UnicornSettingsElement(ConfigElement parent) : base(parent) 
    { 

    } 
    public String HornSize 
    { 
     get 
     { 
      return (String)this["HornSize"]; 
     } 
     set 
     { 
      this["HornSize"] = value; 
     } 
    } 
    public String HoofColor 
    { 
     get 
     { 
      return (String)this["HoofColor"]; 
     } 
     set 
     { 
      this["HoofColor"] = value; 
     } 
    } 
} 

但即使明確實例SpecialConfig.UnicornSettings,它仍然是空:

UnicornSettings config = Config.Get<UnicornSettings>(); 
config.UnicornSettings = new UnicornSettingsElement(config); 
config.UnicornSettings.HornSize = HornSize; //<-- config.UnicornSettings is null 
config.UnicornSettings.HoofColor = HoofColor; 

ConfigManager manager = ConfigManager.GetManager(); 
manager.SaveSection(config); 

我不知道如何克服這個特殊的異常,其中引用設置後立即爲空。任何人都能看到我失蹤的東西?

更新

進一步擺弄之後,我覺得有一些錯誤的SpecialConfig.UnicornSettings的getter或setter ......我不知道,可能是雖然。


免責聲明

我明白了一個空引用異常是什麼,一般來說如何識別和克服一個空引用異常。這不是一個特定的C#問題的重複,其答案是一個非特定的信息。這是一個特殊而確切的案例,涉及一個保證自己的問題的具體框架。


回答

1

忘記ConfigurationProperties。我猜這是必要的方式的getter/setter訪問屬性:

public class SpecialConfig : ConfigSection 
{ 
    [ConfigurationProperty("UnicornSettings")] 
    public UnicornSettingsElement UnicornSettings 
    { 
     get 
     { 
      return (UnicornSettingsElement)this["UnicornSettings"]; 
     } 
     set 
     { 
      this["UnicornSettings"] = value; 
     } 
    } 
} 

public class UnicornSettingsElement : ConfigElement 
{ 
    public UnicornSettingsElement(ConfigElement parent) : base(parent) 
    { 

    } 
    [ConfigurationProperty("HornSize", IsRequired = true)] 
    public String HornSize 
    { 
     get 
     { 
      return (String)this["HornSize"]; 
     } 
     set 
     { 
      this["HornSize"] = value; 
     } 
    } 
    [ConfigurationProperty("HoofColor", IsRequired = true)] 
    public String HoofColor 
    { 
     get 
     { 
      return (String)this["HoofColor"]; 
     } 
     set 
     { 
      this["HoofColor"] = value; 
     } 
    } 
} 
+0

我認爲還需要對 「UnicornSettings」 TypeConvertor'[的TypeConverter(typeof運算(TypeNameConverter))]' –

相關問題