zoukankan      html  css  js  c++  java
  • 反射类的属性和属性值

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Reflection;
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ReflectToObject.ReflectToStudent();
                //Console.ReadLine();
                Console.WriteLine();
                ReflectToObject.ReflectStudents();
                Console.WriteLine();
                ReflectToObject.ReflectClass();
                Console.WriteLine();
                Console.ReadLine();
            }
        }
        class ReflectToObject
        {
            public static void ReflectToStudent()
            {
                Student s = new Student();
                s.Name = "lilei";
                s.Age = 25;
                s.Sex = "nv";
                Type type = s.GetType();
                PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (PropertyInfo field in properties)
                {
                    Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", field.PropertyType, field.Name, field.GetValue(s, null).ToString());
                }
            }
            public static void ReflectStudents()
            {
                Students c = new Students();
                Student sLiLei = new Student();
                sLiLei.Name = "liLei";
                sLiLei.Age = 20;
                sLiLei.Sex = "Femail";
                c.LiLei = sLiLei;
                Student sHanMeimei = new Student();
                sHanMeimei.Name = "HanMeimei";
                sHanMeimei.Age = 22;
                sHanMeimei.Sex = "Mail";
                c.HanMeimei = sHanMeimei;
                Type t = c.GetType();
                foreach (PropertyInfo field in t.GetProperties())
                {
                    Student s = (Student)field.GetValue(c, null);
                    foreach (PropertyInfo p in s.GetType().GetProperties())
                    {
                        Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", p.PropertyType, p.Name, p.GetValue(s, null).ToString());
                    }
                }
            }
            public static void ReflectClass()
            {
                Class c = new Class();
                List<Student> lS = new List<Student>();
                Student sLiLei = new Student();
                sLiLei.Name = "liLei";
                sLiLei.Age = 20;
                sLiLei.Sex = "Femail";
                lS.Add(sLiLei);
                Student sHanMeimei = new Student();
                sHanMeimei.Name = "HanMeimei";
                sHanMeimei.Age = 22;
                sHanMeimei.Sex = "Mail";
                lS.Add(sHanMeimei);
                c.Students = lS;
                c.ClassName = "计算机科学与技术";
                Type t = c.GetType();
                foreach (PropertyInfo field in t.GetProperties())
                {
                    if (field.PropertyType == (new List<Student>()).GetType())
                    {
                        List<Student> st = (List<Student>)field.GetValue(c, null);
                        foreach (Student s in st)
                        {
                            //Student s = (Student)field.GetValue(c, null);
                            foreach (PropertyInfo p in s.GetType().GetProperties())
                            {
                                Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", p.PropertyType, p.Name, p.GetValue(s, null).ToString());
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("属性类型:{0},属性名:{1},属性值:{2}", field.PropertyType, field.Name, field.GetValue(c, null).ToString());
                    }
                   
                }
            }
        }
        class Class
        {
            private string mClassName;
            public string ClassName
            {
                get
                {
                    return mClassName;
                }
                set
                {
                    mClassName = value;
                }
            }
            private List<Student> mStudents;
            public List<Student> Students
            {
                get
                {
                    return mStudents;
                }
                set
                {
                    mStudents = value;
                }
            }
            public Class()
            {
                mStudents = new List<Student>();
            }
        }
        class Students
        {
            private Student mLiLei;
            public Student LiLei
            {
                get
                {
                    return mLiLei;
                }
                set
                {
                    mLiLei = value;
                }
            }
            private Student mHanMeimei;
            public Student HanMeimei
            {
                get
                {
                    return mHanMeimei;
                }
                set
                {
                    mHanMeimei = value;
                }
            }
            public Students()
            {
                mLiLei = new Student();
                mHanMeimei = new Student();
            }
        }
        class Student
        {
            private string mName = "";
            public string Name { get; set; }
            public int Age { get; set; }
            public string Sex { get; set; }
        }
    }
  • 相关阅读:
    10.汇编语言--伪指令(offset、prt、lengthof)
    9.汇编语言--算数运算,标记寄存器
    8.汇编语言--数据传输指令mov等
    7.汇编语言--定义数据类型、数组
    6.汇编语言--汇编基本元素、寄存器、内存初步调试
    5.汇编语言--输入输出
    4.汇编语言--更为简便的使用win32-api
    3.汇编语言--x86处理器架构
    微信小程序获取input输入框里面的value值
    学习微信小程序遇到的坑 ---跳转
  • 原文地址:https://www.cnblogs.com/TNTZWC/p/2422487.html
Copyright © 2011-2022 走看看