1
class Section
{
    public string       Name;
    public List<Option> Options;

    public class Option
    {
        public string Name;
        public string Value;
        public string Path
        {
            get { return "SectionName.OptionName=Value"; }
        }
    }
}

换句话说,我想从 Option 中访问 Section 的成员,该成员将始终用作列表。什么是实现这一点的优雅方法?

4

1 回答 1

4

AnOption需要对其的引用Section

class Section
{
    public string       Name;
    public List<Option> Options;

    public class Option
    {
        public Section MySection { get; private set; }
        public string Name;
        public string Value;
        public string Path
        {
            get { return string.Format("{0}.{1}={2}", this.MySection.Name, this.Name, this.Value); }
        }

        public Option(Section mySection)
        {
            this.MySection = mySection;
        }
    }
}
于 2012-12-27T15:32:45.447 回答