zoukankan      html  css  js  c++  java
  • [C#]通过反射访问类私有成员

    参考链接:

    https://www.cnblogs.com/adodo1/p/4328198.html

    代码如下:

     1 using System;
     2 using System.Reflection;
     3 using UnityEngine;
     4 
     5 public static class ObjectExtension
     6 {
     7     //获取私有字段的值
     8     public static T GetPrivateField<T>(this object instance, string fieldName)
     9     {
    10         BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
    11         Type type = instance.GetType();
    12         FieldInfo info = type.GetField(fieldName, flags);
    13         return (T)info.GetValue(instance);
    14     }
    15 
    16     //设置私有字段的值
    17     public static void SetPrivateField(this object instance, string fieldName, object value)
    18     {
    19         BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
    20         Type type = instance.GetType();
    21         FieldInfo info = type.GetField(fieldName, flags);
    22         info.SetValue(instance, value);
    23     }
    24 
    25     //获取私有属性的值
    26     public static T GetPrivateProperty<T>(this object instance, string propertyName)
    27     {
    28         BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
    29         Type type = instance.GetType();
    30         PropertyInfo info = type.GetProperty(propertyName, flags);
    31         return (T)info.GetValue(instance, null);
    32     }
    33 
    34     //设置私有属性的值
    35     public static void SetPrivateProperty<T>(this object instance, string propertyName, object value)
    36     {
    37         BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
    38         Type type = instance.GetType();
    39         PropertyInfo info = type.GetProperty(propertyName, flags);
    40         info.SetValue(instance, value, null);
    41     }
    42 
    43     //调用私有方法
    44     public static object CallPrivateMethod(this object instance, string methodName, params object[] param)
    45     {
    46         BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
    47         Type type = instance.GetType();
    48         MethodInfo info = type.GetMethod(methodName, flags);
    49         return info.Invoke(instance, param);
    50     }
    51 }
    52 
    53 public class TestReflectionClass {
    54     private int field;
    55     private string Property { get; set; }
    56 
    57     public TestReflectionClass()
    58     {
    59         field = 1;
    60         Property = "a";
    61     }
    62 
    63     private void SetValue(int field, string property)
    64     {
    65         this.field = field;
    66         this.Property = property;
    67     }
    68 }
    69 
    70 public class TestReflection : MonoBehaviour {
    71 
    72     void Start ()
    73     {
    74         TestReflectionClass testClass = new TestReflectionClass();
    75 
    76         print(testClass.GetPrivateField<int>("field"));//1
    77         print(testClass.GetPrivateProperty<string>("Property"));//a
    78 
    79         testClass.CallPrivateMethod("SetValue", 2, "b");
    80 
    81         testClass.SetPrivateProperty<string>("Property", "c");
    82         print(testClass.GetPrivateField<int>("field"));//2
    83         print(testClass.GetPrivateProperty<string>("Property"));//c
    84     }
    85 }
  • 相关阅读:
    "error while loading shared libraries: xxx.so.x" 错误的原因和解决办法 java程序员
    Android巴士转发 java程序员
    好记性不如烂笔头之 ——CP命令 java程序员
    linux之移植内核linux2.6.32psp03.00.01.06 编译出错 java程序员
    eoeAndroid社区转发 java程序员
    id 与 class的区别
    ASP+ACCESS转成ASP+SQL程序应如何修改
    怎么样才能让层显示在FLASH之上呢
    用Javascript作消息提示框(类似于QQ用户上线的消息提示)
    改善用户体验之Alert提示效果
  • 原文地址:https://www.cnblogs.com/lyh916/p/9221855.html
Copyright © 2011-2022 走看看