0

私はこれをずっと見続けてきました。私は愚かなことをしているだけだと知っていますが、それを見ることができません。例外を追加する前は、コレクションは常にnullでした。追加後は常にスローされます。私は何が欠けていますか?

次のようなWeb構成コレクションがあります。

 <section name="WorkOutGroups" type="System.Configuration.WorkOutGroupsSection"/>

<WorkoutGroups>
    <Workouts>
        <add url="something.asp" />
        <add url="/subfolder/another.asp" />
        <add url="../default.asp" />
    </Workouts>
</WorkoutGroups>

私のクラスは次のようになります。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;

namespace MySite.Configuration
{
    public class WorkOutsGroupsSection : ConfigurationSection
    {
        [ConfigurationProperty("WorkOut", IsDefaultCollection = false)]
        [ConfigurationCollection(typeof(WorkOutCollection))]
        public WorkOutCollection WorkOut
        {
            get
            {
                var WorkOut = (WorkOutCollection)base["WorkOut"];
                return WorkOut;
            }
        }
    }

    public class WorkOutCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new WorkOutElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            object result = null;
            if (element is WorkOutElement)
            {
                result = ((WorkOutElement)element).WorkOut;
            }
            return result;
        }

        public WorkOutElement this[int index]
        {
            get { return (WorkOutElement)BaseGet(index); }
        }
    }

    public class WorkOutElement : ConfigurationElement
    {
        [ConfigurationProperty("url", IsRequired = true, IsKey = true)]
        public string WorkOut
        {
            get
            {
                return (string)this["url"];
            }
            set
            {
                this["url"] = value;
            }
        }
    }

    public static class WorkOutsGroups
    {
        static WorkOutsGroupsSection WorkOutsGroupsSection
        {
            get
            {
                var section = ConfigurationManager.GetSection("WorkOutsGroups")
                    as WorkOutsGroupsSection;
                if (section != null)
                {
                    return section;
                }
                throw new ConfigurationErrorsException("WorkOutsGroups Configuration not found");
            }
        }

        public static IEnumerable<string> GetWorkOut()
        {
            var results = WorkOutsGroupsSection.WorkOut
                     .OfType<WorkOutElement>()
                     .Select(p => p.WorkOut);

            return results.ToArray();
        }
    }
}

そして私はそれを次のように実装しています:

foreach (var url in Workout.GetWorkout())
{
    if (link.Value.Contains(url.ToString()))
        returnValue = true;
}
4

2 に答える 2

0

あなたが持っている

var WorkOut = (WorkOutCollection)base["WorkOut"];

C#コードではありますが、構成にはWorkOutがあります。

多分それが理由です。

于 2013-02-19T12:40:34.937 に答える
0

つづり:

ConfigurationManager.GetSection("WorkOutsGroups")

セクション名WorkOutGroupsWorkOutsGroups

于 2013-02-19T12:38:54.803 に答える