c# reflection and attributes

Reflection is generally used in the development of the underlying framework, and all members of the class can be obtained through reflection.

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

namespace 反射和特性
{
    class Program
    {
        static void Main(string[] args)
        {
            //通过Type对象可以获取类中的所有成员(public)
            MyClass my = new MyClass();
            Type type = my.GetType();
            Console.WriteLine(type.Name);//type的名字
            Console.WriteLine(type.Namespace);//type的命名空间
            Console.WriteLine(type.Assembly);//type所在程序集
            FieldInfo[] array = type.GetFields();//获得类中的字段
            foreach(FieldInfo info in array)
            {
                Console.WriteLine(info.Name+"");
            }
            PropertyInfo[] array2 = type.GetProperties();//获得类中的属性
            foreach (PropertyInfo info in array2)
            {
                Console.WriteLine(info.Name + "");
            }
            MethodInfo[] array3 = type.GetMethods();//获得类中的方法
            foreach (MethodInfo info in array3)
            {
                Console.WriteLine(info.Name + "");
            }



            Console.ReadKey();
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_52397831/article/details/126959967