最近在学习Unity的Excel数据读取时,看见有人说可以通过反射来给每个Excel表进行不同类的数据存储,出于好奇,便去学习了一下反射相关的知识,以下算是有关反射的一点点个人学习向总结吧
基础版遍历类成员
using System;
using System.Reflection;//使用反射时必须引入的系统库
namespace test
{
//测试类
class Test
{
//类属性
private string name;
private string sex;
//想通过反射获取类成员的属性的话,该属性必须具有Get/Set方法,否则无法获取
public string Name{get => name;set => name = value;}
public string Sex{get => sex; set => sex = value;}
}
class program
{
public static void Main(string[] args)
{
//创建类对象
Test test = new Test();
//通过Type类获取该类对象的对应的类信息
Type type = test.GetType();
//通过type获取该类中的属性
PropertyInfo[] propertys = type.GetProperties();