2008-05-16

C# Reflection

关键字: c# reflection

using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace detegate
{


    class Data
    {
        //成绩数据以字段呈现
        public Score score1;
        public Score score2;
        public Score score3;
        public Score score4;

        //学生数据以属性呈现
        private Student student1;
        private Student student2;

        //课程数据以方法返回
        private Course course1;
        private Course course2;

        //属性封装了方法
        public Student Student1
        {
            get { return student1; }

        }
        public Student Student2
        {
            get { return student2; }

        }

        // 初始化数据

        public Data()
        {
            student1 = new Student("小猪",Gender.男);
            student2 = new Student("小成",Gender.女);

            course1 = new Course("课程A","老王");
            course2 = new Course("课程B","老李");

            score1 = new Score(student1,course1,91);
            score2 = new Score(student1, course2, 81);

            score3 = new Score(student2, course1, 92);
            score4 = new Score(student2, course2, 82);

        }

        //返回课程数据
        public Course getCourse1()
        {
            return course1;
        }
        public Course getCourse2()
        {
            return course2;
        }

    }

    enum Gender
    {
        男,
        女
    }


    class Student
    {
        public string studentName;
        public Gender studentGender;

        public Student(string studentName, Gender studentGender)
        {
            this.studentName = studentName;
            this.studentGender = studentGender;
        }

    }


    class Course
    {
        public string courseName;
        public string courseTeacherName;

        public Course(string courseName, string courseTeacherName)
        {
            this.courseName = courseName;
            this.courseTeacherName = courseTeacherName;

        }
    }

    class Score
    {
        public Student student;
        public Course course;
        public int mark;

        public Score(Student student, Course course,int  mark)
        {
            this.student = student;
            this.course = course;
            this.mark = mark;
        }

    }

    class ReflectionHelper
    {

        // 使用反射遍历对象的成员

        public void listMenber(Data date)
        {
            Console.WriteLine("\n----------------------遍历对象的成员------------------------------");
            Console.WriteLine("\n成员名\t\t成员类型\t\t声明成员的类");
            //表示获取成员的MemberInfo数组
            MemberInfo[] arr = date.GetType().GetMembers();
            foreach(MemberInfo   mi  in  arr)
            {
                Console.WriteLine(string.Format ("{0}\t{1}\t{2}",
                    mi.Name.PadRight(10,' '),
                    mi.MemberType.ToString().PadRight(10, ' '),
                    mi.DeclaringType .ToString ().PadRight(10,' ')));
            }
        }

        //使用反射遍历对象的字段
        public void ListField(Data data)
        {
            Console.WriteLine("\n---------------------遍历对象字段----------------------------------");
            Console.WriteLine("\n姓名\t性别\t课程名\t授课老师\t成绩");
            // 获取表示字段的FiledInfo类型的数组
            FieldInfo[] arr = data.GetType().GetFields();
            foreach (FieldInfo fi in arr)
            {
                Score score = (Score)fi.GetValue(data);
                Console.WriteLine(string.Format ("{0}\t{1}\t{2}\t{3}\t{4}",
                    score.student.studentName,
                    score.student.studentGender,
                    score.course.courseName,
                    score.course.courseTeacherName,
                    score.mark));
            }

        }

        //使用反射遍历对象的属性
        public void ListProperty(Data data)
        {
            Console.WriteLine("\n---------------------遍历对象属性----------------------------------");
            Console.WriteLine("\n姓名\t性别");
            //获取属性 PropertyInfo数组
            PropertyInfo[]  arr =  data.GetType().GetProperties();

             foreach(PropertyInfo  pi in    arr)
             {
                 //读取字段的值
                 Student  student  = (Student) pi.GetValue(data,null);
                 Console.WriteLine(string.Format ("{0}\t{1}",student.studentName,student.studentGender));
             }
        }

        //使用反射遍历对象的方法
        public void ListMethod(Data data)
        {
            Console.WriteLine("\n---------------------遍历对象方法----------------------------------");
            Console.WriteLine("\n课程名\t授课老师");
            //  获取方法数组
            MethodInfo[] arr = data.GetType().GetMethods();
            foreach (MethodInfo mi in arr)
            {
                if (mi.Name.IndexOf("getCourse") > -1)
                {
                  Course cource = mi.Invoke(data,null)  as  Course;
                   Console.WriteLine(string.Format("{0}\t{1}",
                       cource.courseName ,
                      cource.courseTeacherName));
                }
            }
        }


      public static  void Main(string[] args)
      {
          Data data = new Data();
          ReflectionHelper reflectHelper = new ReflectionHelper();
          reflectHelper.listMenber(data);
          reflectHelper.ListField(data);
          reflectHelper.ListProperty(data);
          reflectHelper.ListMethod(data);
      }
    }
}

 

  • 9bf55c92-f066-34c8-8fbe-ecf273ce0b20-thumb
  • 描述: 运行结果图
  • 大小: 52.5 KB
评论
sonnywanger 2008-06-05
  • sdfgsdf
ds
ddddddddddddddddddddddfghttp://sadf
wsq777 2008-05-17
路过。。。。
发表评论

您还没有登录,请登录后发表评论

isky
搜索本博客
我的相册
B4a53845-f18b-3b2c-8f27-319d394b08e1-thumb
SL372159
共 29 张
存档
最新评论