using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Reflector_Type
{
class Program
{
static void Main(string[] args)
{
/*
Type类
*/
//1.怎么获取一个类型的Type(该类型的类型元数据)
MyClass m = new MyClass();
//获得了类型MyClass的对应Type(有对象)
Type t1 = m.GetType();
//获得了类型MyClass的对应Type(通过Typeof关键字,无须获取Myclass类型对象就可以拿到MyclassType对象)
Type t2 = typeof(MyClass);
//2.拿到Type类能干什么?
Type TypeMyclass = typeof(MyClass);
//可以获取当前的父类 TypeMyclass.BaseType.ToString()
Console.WriteLine(TypeMyclass.BaseType.ToString());
//获取当前类型中所有的字段信息
//这里只能获取非私有的字段信息
FieldInfo [] fileds = TypeMyclass.GetFields();
for (int i = 0; i < fileds.Length; i++)
{
Console.WriteLine(fileds[i].Name.ToString());
}
//获取当前类型中的所有属性
PropertyInfo [] propinfo = TypeMyclass.GetProperties();
for (int i = 0; i < propinfo.Length; i++)
{
Console.WriteLine(propinfo[i].Name.ToString());
}
//获取Type中的方法
MethodInfo [] Methods = TypeMyclass.GetMethods();
for (int i = 0; i < Methods.Length; i++)
{
Console.WriteLine(Methods[i].Name.ToString());
}
//Z获取Type中所有的成员
MemberInfo [] Members = TypeMyclass.GetMembers();
Console.ReadKey();
}
}
public class MyClass
{
public int field1;
public string field2;
public string Name { get; set; }
public string Address { get; set; }
public void Say()
{
Console.WriteLine("十方无影像,六道绝踪迹");
}
private void Say2()
{
Console.WriteLine("跳出三界外,不在五行中");
}
}
}