C#反射的基本应用

1.使用反射需引用   using System.Reflection;

2.创建用于反射的对象

class People
{
    string _name = "1";   //私有字段

    public string Name = "2";  //公共字段

    public int Age { get; set; } = 3;  //公共属性
    int _age { get; set; } = 4;  //私有属性

    public static string Location { get; set; } = "6";

    public People(int age)   //带参构造函数
    {
        Age = age;
    }

    public People()   //无参构造函数
    {
    }

    string getName()  //私有方法
    {
        return _name;
    }

    public void Clear()
    {
        Age = 8;
    }

    public int GetAge()  //公共方法
    {
        return Age;
    }

    public int GetAge(int age)
    {
        return age;
    }

    public int GetAge(int age, out string location)
    {
        location = "RE";
        return age;
    }

}

3. 动态创建对象

//动态创建对象1
People p1 = (People)Activator.CreateInstance(typeof(People));

//动态创建对象2
Type tt = Type.GetType("ConsoleApp7.People");   //类似的 (名称空间.类名)
People p2 = (People)Activator.CreateInstance(tt);

//动态创建对象3
Assembly asm = Assembly.LoadFile(@"C:\repos\WindowsFormsApp1\Use\bin\Debug\Use.dll"); //dll绝对路径
Type type = asm.GetType("BLL_USE.People");  //对应的  (名称空间.类名)
object p3 = Activator.CreateInstance(type);  //创建一个object类型对象

4.获取反射对象中的成员 

   4.1 获取公共成员  public  (包括静态的  static

Type type = typeof(People);
//只能获取可外部访问的
PropertyInfo[] propertyInfos = type.GetProperties();  //获取属性
FieldInfo[] fieldInfos = type.GetFields();  //获取字段
MemberInfo[] memberInfos = type.GetMembers();   //获取成员
MethodInfo[] methodInfos = type.GetMethods();   //获取方法
ConstructorInfo[] constructorInfos = type.GetConstructors();  //获取构造函数

  4.2 获取非公共成员  private、internal、protected 

//获取非静态的非公共方法,其他类似
MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);  

//获取静态的非公共方法,其他类似
MethodInfo[] methodInfos = type.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); 

 5.对反射对象成员的基本操作

//获取无参构造函数实例化对象
ConstructorInfo constructorInfo = type.GetConstructor(new Type[] { });
object p00 = constructorInfo.Invoke(null);

//获取有参构造函数实例化对象
ConstructorInfo constructorInfo2 = type.GetConstructor(new Type[] { typeof(int) });
object p11 = constructorInfo2.Invoke(new object[] { 100 });

People p22 = new People() { Age = 45 };   //针对非静态成员
object age = type.GetProperty("Age").GetValue(p22);  //45  //从哪个对象里面获取public值
type.GetProperty("Age").SetValue(p22, 65);   //类似于:p22.Age = 65;

//获取public static值,可以不指定对象
object location = type.GetProperty("Location").GetValue(null);  //结果:"6"  

//获取internal static值,可以不指定对象
//object location = type.GetProperty("Location", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);

//改变静态属性值
type.GetProperty("Location").SetValue(null, "F");  //类似于:p22.Location = "F";  

//执行无参无返回方法
type.GetMethod("Clear").Invoke(p22, null); //类似于:p22.Clear(); 

//执行无参有返回的重载方法
object age1 = type.GetMethod("GetAge", new Type[] { }).Invoke(p22, null);  //类似于:object age1 = p22.GetAge();   

//执行有参有返回的重载方法
object age2 = type.GetMethod("GetAge", new Type[] { typeof(int) }).Invoke(p22, new object[] { 444 }); //类似于:object age1 = p22.GetAge(444);   

//执行有参有返回的重载方法 (带out或者ref参数)
object[] invokeArgs = new object[] { 36, "444" };
object age3 = type.GetMethod("GetAge", new Type[] { typeof(int), typeof(string).MakeByRefType() }).Invoke(p22, invokeArgs); //类似于:object age1 = p22.GetAge(36,out "444");
//结果:invokeArgs [0] = 36 ,invokeArgs [1] = "RE"

    PS: 以上都可以对非公共成员操作,需加上  BindingFlags.Instance | BindingFlags.NonPublic 

//获取非公共成员值
object age = type.GetField("_name", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(p22);  //结果:1  

 

 

 

posted @ 2020-01-09 10:51  Bridgebug  阅读(159)  评论(0编辑  收藏  举报