- Home /
 
 
               Question by 
               haruna9x · Jul 31, 2017 at 09:50 AM · 
                c#variablecustom-editorprivate  
              
 
              How to keep private variables, while being able to access them in the custom EditorWindow.
I am making an editor for character stats. However, I would be forced to set the variables to public, or at least internal. How can I keep them private.
I am working here:
  public class EntityDatabaseEditor : EditorWindow
     {
  [MenuItem("Window/GameSystems/EntityDatabase")]
         static void Init()
         {
             EditorWindow window = GetWindow(typeof(EntityDatabaseEditor));
             window.minSize = new Vector2(600, 400);
             window.Show();
         }
 }
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by haruna9x · Aug 29, 2017 at 05:00 PM
I was able to access private members by Reflection:
 public static class EditorReflection
     {
         /// <summary>
         /// Returns a _private_ Property Value from a given Object. Uses Reflection.
         /// Throws a ArgumentOutOfRangeException if the Property is not found.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is returned</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <returns>PropertyValue</returns>
         public static T GetProperty<T>(this object obj, string propName)
         {
             if (obj == null) throw new ArgumentNullException("obj");
             PropertyInfo pi = obj.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
             if (pi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
             return (T)pi.GetValue(obj, null);
         }
 
         /// <summary>
         /// Returns a private Property Value from a given Object. Uses Reflection.
         /// Throws a ArgumentOutOfRangeException if the Property is not found.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is returned</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <returns>PropertyValue</returns>
         public static T GetField<T>(this object obj, string propName)
         {
             if (obj == null) throw new ArgumentNullException("obj");
             Type t = obj.GetType();
             FieldInfo fi = null;
             while (fi == null && t != null)
             {
                 fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                 t = t.BaseType;
             }
             if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName));
             return (T)fi.GetValue(obj);
         }
 
         /// <summary>
         /// Sets a _private_ Property Value from a given Object. Uses Reflection.
         /// Throws a ArgumentOutOfRangeException if the Property is not found.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is set</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <param name="val">Value to set.</param>
         /// <returns>PropertyValue</returns>
         public static void SetProperty<T>(this object obj, string propName, T val)
         {
             Type t = obj.GetType();
             if (t.GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) == null)
                 throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
             t.InvokeMember(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.SetProperty | BindingFlags.Instance, null, obj, new object[] { val });
         }
 
         /// <summary>
         /// Set a private Property Value on a given Object. Uses Reflection.
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="obj">Object from where the Property Value is returned</param>
         /// <param name="propName">Propertyname as string.</param>
         /// <param name="val">the value to set</param>
         /// <exception cref="ArgumentOutOfRangeException">if the Property is not found</exception>
         public static void SetField<T>(this object obj, string propName, T val)
         {
             if (obj == null) throw new ArgumentNullException("obj");
             Type t = obj.GetType();
             FieldInfo fi = null;
             while (fi == null && t != null)
             {
                 fi = t.GetField(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                 t = t.BaseType;
             }
             if (fi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Field {0} was not found in Type {1}", propName, obj.GetType().FullName));
             fi.SetValue(obj, val);
         }
 
         /// <summary>
         /// Get name a property as a string
         /// </summary>
         /// <typeparam name="T">Type of the Property</typeparam>
         /// <param name="propertyLambda">Lambda of the form: '() => Class.Property' or '() => object.Property'</param>
         /// <returns></returns>
         public static string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
         {
             var me = propertyLambda.Body as MemberExpression;
 
             if (me == null)
             {
                 throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
             }
 
             return me.Member.Name;
         }
     }
 
               Example:
 EditorReflection.SetProperty(item, EditorReflection.GetPropertyName(() => item.Name), 
                                              EditorGUILayout.TextField("Name", item.Name));
 
              Your answer