#if UNITY_EDITOR using System; using System.Linq; using System.Reflection; namespace BrainFailProductions.PolyFew { public class PrivateValueAccessor { public static BindingFlags Flags = BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.GetField | BindingFlags.SetField | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Default | BindingFlags.InvokeMethod; /// /// A static method to get the PropertyInfo of a private property of any object. /// /// The Type that has the private property /// The name of the private property /// PropertyInfo object. It has the property name and a useful GetValue() method. public static PropertyInfo GetPrivatePropertyInfo(Type type, string propertyName) { var props = type.GetProperties(Flags); return props.FirstOrDefault(propInfo => propInfo.Name == propertyName); } /// /// A static method to get the value of a private property of any object. /// /// The Type that has the private property /// The name of the private property /// The instance from which to read the private value. /// The value of the property boxed as an object. public static object GetPrivatePropertyValue(Type type, string propertyName, object o) { return GetPrivatePropertyInfo(type, propertyName).GetValue(o, Flags, null, null, null); } /// /// A static method to get the FieldInfo of a private field of any object. /// /// The Type that has the private field /// The name of the private field /// FieldInfo object. It has the field name and a useful GetValue() method. public static FieldInfo GetPrivateFieldInfo(Type type, string fieldName) { var fields = type.GetFields(Flags); return fields.FirstOrDefault(fieldInfo => fieldInfo.Name == fieldName); } /// /// A static method to get the FieldInfo of a private field of any object. /// /// The Type that has the private field /// The name of the private field /// The instance from which to read the private value. /// The value of the property boxed as an object. public static object GetPrivateFieldValue(Type type, string fieldName, object o) { return GetPrivateFieldInfo(type, fieldName).GetValue(o); } } } #endif