Как добавить свойство ICollection при повторении типа объекта?

Мне нужно добавить к свойству ICollectionкласса, для которого у меня есть IEnumerable. Вот полная программа, которая иллюстрирует проблему:

using System;
using System.Collections.Generic;
using System.Linq;

namespace CollectionAddingTest
{
    public class OppDocumentServiceResult
    {
        public OppDocumentServiceResult()
        {
            this.Reasons = new List<string>();
        }

        public Document Document { get; set; }

        public bool CanBeCompleted
        {
            get
            {
                return !Reasons.Any();
            }
        }

        public ICollection<string> Reasons { get; private set; }
    }

    public class Document
    {
        public virtual string Name { get; set; }
    }

    public class Program
    {
        private static void Main(string[] args)
        {
            var docnames = new List<string>(new[] {"test", "test2"});

            var oppDocResult = docnames
                .Select(docName
                        => new OppDocumentServiceResult
                               {
                                   Document = new Document { Name = docName }
                               });

            foreach (var result in oppDocResult)
            {
                result.Document.Name = "works?";
                result.Reasons.Add("does not stick");
                result.Reasons.Add("still does not stick");
            }

            foreach (var result in oppDocResult)
            {
                // doesn't write "works?"
                Console.WriteLine(result.Document.Name);

                foreach (var reason in result.Reasons)
                {
                    // doesn't even get here
                    Console.WriteLine("\t{0}", reason);
                }
            }
        }
    }
}

Я ожидаю, что каждый OppDocumentServiceResult будет иметь ссылку на Document.Name свойство, установленное на , работает?, и к каждому OppDocumentServiceResult должны быть добавлены две причины. Однако ни то, ни другое не происходит.

Что особенного в свойстве Reasons, что я не могу добавлять к нему что-то?

0
задан Russ Clark 15 May 2012 в 16:29
поделиться