反射是Net提供的动态调用生成对象,动态调用方法的一组类库


新建一个Relection项目,增加一个Model类库如下:

反射(七)_第1张图片

在Model类库新增Student类

public class Student
    {
        private int _id;
        private string _name;
        private int _age;
        public Student()
        {
            Console.WriteLine("Student Initializtion");
        }
        public int Id
        { get { return _id; }
            set
            {
                _id = value;
            }
        }
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
            }
        }
        public int Age
        {
            get { return _age; }
            set
            {
                _age = value;
            }
        }
        public void Learn()
        {
            Console.WriteLine("Student begin study");
        }
        private void Play()
                {
                    Console.WriteLine("Student Begin Play");
                }
        
                public void SetAge(int age)
                {
                    this._age = age;
                }
    }

在Program.cs

class Program
    {
        static void Main(string[] args)
        {
            //读取Model类库
            var model = Assembly.Load("Model");
            Console.WriteLine("*************Modules**************");
            foreach (var item in model.GetModules())//获取模块
            {
                Console.WriteLine(item.FullyQualifiedName);
            }
            Console.WriteLine("*************Types**************");
            foreach (var item in model.GetTypes())//获取所有类型
            {
                Console.WriteLine(item.FullName);
            }
            Type type = model.GetType("Model.Student");
            Console.WriteLine("*************Properties**************");
            foreach (var item in type.GetProperties())//获取所有所有属性
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("*************Fields**************");
            foreach (var item in type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance))//获取所有所有字段,必须加上BindingFlags.NonPublic | BindingFlags.Instance
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("*************Methods**************");//获取所有所有方法
            foreach (var item in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance|BindingFlags.Public))
            {
                Console.WriteLine(item.Name);
            }
            Console.WriteLine("*************student**************");
            var student = Activator.CreateInstance(type);//创建一个Student对象
            Console.WriteLine("*************student Properties**************");
            foreach (var item in type.GetProperties())//获取Student所有属性并赋值
            {
                if (item.Name == "Id") item.SetValue(student, 1);
                if(item.Name=="Name") item.SetValue(student, "cc");
                if (item.Name == "Age") item.SetValue(student, 27);
                Console.WriteLine(item.Name + "=" + item.GetValue(student));
            }
            Console.WriteLine("*************student Methods**************");
            foreach (var item in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))//获取Student所有属性方法并调用
            {
                if (item.Name == "Learn") item.Invoke(student,null);
                if(item.Name=="Play") item.Invoke(student, null);
                if (item.Name == "SetAge") item.Invoke(student, new object[] { 28});
            }
            Console.WriteLine("修改后的Age="+type.GetProperty("Age").GetValue(student));
            Console.ReadKey();
        }
    }

输出结果:

反射(七)_第2张图片