日期:2014-05-17  浏览次数:21359 次

.net 只读属性问题

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Test t = new Test
            {
                TestField = { new Student { Name = "ss", Age = 111 } }//TestField是只读的 怎么 还能这么写
            };
        }
    }

    public class Student 
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    public class Test 
    {
        public StuCollection _testField;
        public  StuCollection TestField 
        {
            get 
            {
                if (_testField == null)
                {
                    _testField = new StuCollection();
                }
                return _testField;
            }
        }
    }

    public class StuCollection : List<Student> 
    {
    }
.net