using System; using System.Reflection; namespace XFEExtension.ReflectionExtension { /// /// 反射拓展 /// public static class ReflectionExtension { /// /// 获取方法默认参数 /// /// /// public static object[] GetDefaultParameters(this MethodInfo method) { var parameters = method.GetParameters(); var defaultParameters = new object[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { var paramType = parameters[i].ParameterType; defaultParameters[i] = paramType.IsValueType ? Activator.CreateInstance(paramType) : null; } return defaultParameters; } /// /// 获取某个类中的某个私有字段的值 /// /// /// /// 字段名称 /// public static T GetPrivateField(this object obj, string fieldName) { var field = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); return (T)field.GetValue(obj); } /// /// 设置某个类中的某个私有字段的值 /// /// /// 字段名称 /// 值 public static void SetPrivateField(this object obj, string fieldName, object value) { var field = obj.GetType().GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); field.SetValue(obj, value); } /// /// 获取某个类中的某个私有属性的值 /// /// /// /// 属性名称 /// public static T GetPrivateProperty(this object obj, string propertyName) { var property = obj.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance); return (T)property.GetValue(obj); } /// /// 设置某个类中的某个私有属性的值 /// /// /// 属性名称 /// 值 public static void SetPrivateProperty(this object obj, string propertyName, object value) { var property = obj.GetType().GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Instance); property.SetValue(obj, value); } /// /// 获取某个类中的某个私有静态字段的值 /// /// /// /// 字段名称 /// public static T GetPrivateStaticField(this Type type, string fieldName) { var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static); return (T)field.GetValue(null); } /// /// 设置某个类中的某个私有静态字段的值 /// /// /// 字段名称 /// 值 public static void SetPrivateStaticField(this Type type, string fieldName, object value) { var field = type.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static); field.SetValue(null, value); } /// /// 获取某个类中的某个私有静态属性的值 /// /// /// /// 属性名称 /// public static T GetPrivateStaticProperty(this Type type, string propertyName) { var property = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Static); return (T)property.GetValue(null); } /// /// 设置某个类中的某个私有静态属性的值 /// /// /// 属性名称 /// 值 public static void SetPrivateStaticProperty(this Type type, string propertyName, object value) { var property = type.GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.Static); property.SetValue(null, value); } /// /// 调用某个类中的某个私有方法 /// /// /// /// 方法名称 /// 传入的参数 /// public static T InvokePrivateMethod(this object obj, string methodName, params object[] parameters) { var method = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); return (T)method.Invoke(obj, parameters); } /// /// 调用某个类中的某个私有方法 /// /// /// 方法名称 /// 传入参数 public static void InvokePrivateMethod(this object obj, string methodName, params object[] parameters) { var method = obj.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(obj, parameters); } /// /// 调用某个类中的某个私有静态方法 /// /// /// /// 方法名称 /// 传入参数 /// public static T InvokePrivateStaticMethod(this Type type, string methodName, params object[] parameters) { var method = type.GetMethod(methodName); return (T)method.Invoke(null, parameters); } } }