System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Instance
无法获取私有属性
System.Reflection.BindingFlags.Public| System.Reflection.BindingFlags.Instance
但可以获取公共属性
怎么反射私有属性
希望对你有帮助:
public static void SetPrivateProperty(this object instance, string propertyname, object value) { BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic; Type type = instance.GetType(); PropertyInfo field = type.GetProperty(propertyname, flag); field.SetValue(instance, value, null); }
System.Reflection.BindingFlags.Public 这样是公有属性
System.Reflection.BindingFlags。NonPublic 就是私有的了
楼上正解