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)
{
//获取反射的方法有两种
//1.有对象使用对象获取反射
//MyClass mc = new MyClass();
//Type type = mc.GetType();
//没有对象使用typeof获取反射
Type type = typeof(MyClass);
//获取所有非私有属性
PropertyInfo[] propertyInfo = type.GetProperties();
for (int i = 0; i < propertyInfo.Length; i++)
{
Console.WriteLine(propertyInfo[i].Name);
}
//获取非私有字段
FieldInfo[] fieldInfos = type.GetFields();
for (int i = 0; i < fieldInfos.Length; i++)
{
Console.WriteLine(fieldInfos[i].Name);
}
MethodInfo methodInfo = type.GetMethod("SayHi");
MethodBody methodBody = methodInfo.GetMethodBody();
Console.WriteLine(methodBody);
Console.ReadKey();
}
}
class MyClass
{
public string _gtfs;
public string _btfs;
public string Name
{
get;
set;
}
public void SayHi()
{
Console.WriteLine("Hi~~~");
}
private void SayHello()
{
Console.WriteLine("Hello~~~");
}
}
}