首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#中通过发射获取对象的所有属性出错, 求指导. 标红部分不能正确获取到language里面的属性值,该如何处理

2013-10-21 
C#中通过发射获取对象的所有属性出错, 求指导.标红部分不能正确获取到language里面的属性值首先定义了一个

C#中通过发射获取对象的所有属性出错, 求指导. 标红部分不能正确获取到language里面的属性值
首先定义了一个student类, 这个类中有有个成员speakLanguage 为language类.
namespace person
{
    class student
    {
        
        public string name { get; set; }

        public string age { get; set; }

        public language speakLanguage { get; set; }

        public string ex { get; set; }
        
    }

    class language
    {
        public string english { get; set; }
        public string chinese { get; set; }
        public string germany { get; set; }
    }
}


            student std= new student();
            std.name = "tina";
            std.age = "100";
            std.ex = "man";
            std.speakLanguage = new language();
            std.speakLanguage.chinese = "yes";
            std.speakLanguage.english = "yes";
            std.speakLanguage.germany = "no";
         {
                object[] row = new object[std.GetType().GetProperties().Length];
                foreach (PropertyInfo property in std.GetType().GetProperties())
                {
                    if (property.PropertyType != typeof(String))                    {
                        PropertyInfo[] pro=property.PropertyType.GetProperties();

                        foreach (PropertyInfo subproperty in pro)
                        {
                            object str = new object();                            str = subproperty.GetValue(property, null);
                            row[i++] = str;
                        }
                    }
                    else
                    {
                        object str = property.GetValue(std, null);
                        row[i++] = str;
                    }
                }
            }


[解决办法]

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

namespace person
{
    class student
    {
        
        public string name { get; set; }

        public string age { get; set; }

        public language speakLanguage { get; set; }

        public string ex { get; set; }
        
    }

    class language
    {
        public string english { get; set; }
        public string chinese { get; set; }
        public string germany { get; set; }
    }

    class Program
    {
        static void Main(string[] Args)
        {
            student std = new student();
            std.name = "tina";
            std.age = "100";
            std.ex = "man";
            std.speakLanguage = new language();
            std.speakLanguage.chinese = "yes";
            std.speakLanguage.english = "yes";
            std.speakLanguage.germany = "no";
            {
                object[] row = new object[std.GetType().GetProperties().Length];
                foreach (PropertyInfo property in std.GetType().GetProperties())
                {
                    if (property.PropertyType != typeof(String))
                    {
                        PropertyInfo[] pro = property.PropertyType.GetProperties();

                        foreach (PropertyInfo subproperty in pro)
                        {
                            object str = new object();
                            str = subproperty.GetValue(property.GetValue(std, null), null);
                            Console.WriteLine(str);
                        }
                    }
                    else
                    {
                        object str = property.GetValue(std, null);
                        Console.WriteLine(str);
                    }
                }
            }
        }


    }
}

热点排行