为什么TypeDescriptor.GetProperties()不适合我?

时间:2014-01-28 19:26:09

标签: c#

我有一些代码可以创建几个Person对象,然后将这些对象添加到List中。我正在尝试使用TypeDescriptor.GetProperties()将属性和值输出到控制台。不过,我没有得到任何输出。当我在调试中单步执行时,描述符foreach循环就像我的Person对象中没有属性一样。但是,当我在调试中查看对象时,我可以看到对象的所有属性,它们都有值(即name =“Mark”,age =“40”等)。有人告诉我为什么它不起作用,显然,我的对象中有属性和值?

以下是我正在使用的代码:

foreach (var person in People)
{
     foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(person))
     {
          string name = descriptor.Name;
          object value = descriptor.GetValue(person);
          Console.WriteLine("{0}={1}", name, value);
     }
}
编辑:这是我正在修补的课程和程序。基本上只是一个程序,您可以将特定的XML文件拖放到表单中,目的是将对象的属性及其值打印到控制台。

这是我的Person类:

namespace WindowsFormsApplication5
{
     public class Person
     {

         //Must have zero parameter constructor for Object => XML serialization.
         public Person(){}

         public Person(string name, int age, string gender, bool hasChildren, 

         List<Person> children = null )
         {
            Name = name;
            Age = age;
            Gender = gender;
            HasChildren = hasChildren;
            Children = children;
        }

        private string Name { get; set; }
        private int Age { get; set; }
        private string Gender { get; set; }
        private bool HasChildren { get; set; }
        private List<Person> Children { get; set; }
    }
}

这是我的计划:

namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var People = new List<Person>();
            var ChildrenList = new List<Person>();
            var hasChildren = false;
            var ofd = new OpenFileDialog { Filter = "XML|*.xml" };
            if (ofd.ShowDialog() != DialogResult.OK) return;

            var xdoc = XDocument.Load(ofd.FileName);
            foreach (var element in xdoc.Descendants("People").Elements("Person"))
            {
                var name = element.Element("Name").Value;
                var age = Convert.ToInt32(element.Element("Age").Value);
                var gender = element.Element("Name").Value;
                var xmlNode = element.Descendants("Children").FirstOrDefault(xe => xe.Elements("Child").Any());
                if (xmlNode != null)
                {
                    hasChildren = true;
                    foreach (var child in element.Descendants("Children").Descendants("Child"))
                    {
                        var ChildName = child.Element("Name").Value;
                        var ChildAge = Convert.ToInt32(child.Element("Age").Value);
                        var ChildGender = child.Element("Gender").Value;
                        ChildrenList.Add(new Person(ChildName, ChildAge, ChildGender, false));
                    }
                }
                People.Add(new Person(name, age, gender, hasChildren, ChildrenList));
            }

            foreach (var person in People)
            {
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(person))
                {
                    string name = descriptor.Name;
                    object value = descriptor.GetValue(person);
                    Console.WriteLine("{0}={1}", name, value);
                }
            }

        }
    }
}

1 个答案:

答案 0 :(得分:5)

Person类的属性是私有的; TypeDescriptor仅返回公共属性。