简体   繁体   中英

Sitefinity config property null, even immediately after being set

I've created a new configuration file Special.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>

Then followed the documentation to set up a pair of classes (and register the config in the Global.asax.cs) file:

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;
        }
    }
}

But even after explicitly instantiating SpecialConfig.UnicornSettings, it's still null:

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);

I have no idea how to overcome this particular exception where the reference is null immediately after being set. Anyone see what I'm missing?

Update

After further fiddling, I think there's something wrong with the getter or setter on the SpecialConfig.UnicornSettings... I'm not sure what that could be though.


DISCLAIMER

I understand what a null reference exception is, and generally speaking how to identify and overcome a null reference exception. This is not a duplicate of a particular C# question whose answer is a very non-specific book of information. This is a particular and precise case involving a specific framework that warrants its own question.


Forgot the ConfigurationProperties. I'm guessing these are necessary for the way the getter/setter accesses properties:

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM