¿Es posible establecer propiedad privada a través de la reflexión?

¿Puedo establecer una propiedad privada a través de la reflexión?

public abstract class Entity { private int _id; private DateTime? _createdOn; public virtual T Id { get { return _id; } private set { ChangePropertyAndNotify(ref _id, value, x => Id); } } public virtual DateTime? CreatedOn { get { return _createdOn; } private set { ChangePropertyAndNotify(ref _createdOn, value, x => CreatedOn); } } } 

He intentado lo siguiente y no funciona, donde t representa un tipo de Entity :

 var t = typeof(Entity); var mi = t.GetMethod("set_CreatedOn", BindingFlags.Instance | BindingFlags.NonPublic); 

Creo que puedo hacer esto pero no puedo resolverlo.

 t.GetProperty("CreatedOn") .SetValue(obj, new DateTime(2009, 10, 14), null); 

EDITAR: dado que la propiedad en sí misma es pública, aparentemente no necesita usar BindingFlags.NonPublic para encontrarla. Llamar a SetValue pesar de que el setter tenga menos accesibilidad sigue haciendo lo que esperas.

Sí lo es:

  ///  /// Returns a _private_ Property Value from a given Object. Uses Reflection. /// Throws a ArgumentOutOfRangeException if the Property is not found. ///  /// Type of the Property /// Object from where the Property Value is returned /// Propertyname as string. /// PropertyValue public static T GetPrivatePropertyValue(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); } ///  /// Returns a private Property Value from a given Object. Uses Reflection. /// Throws a ArgumentOutOfRangeException if the Property is not found. ///  /// Type of the Property /// Object from where the Property Value is returned /// Propertyname as string. /// PropertyValue public static T GetPrivateFieldValue(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); } ///  /// Sets a _private_ Property Value from a given Object. Uses Reflection. /// Throws a ArgumentOutOfRangeException if the Property is not found. ///  /// Type of the Property /// Object from where the Property Value is set /// Propertyname as string. /// Value to set. /// PropertyValue public static void SetPrivatePropertyValue(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 }); } ///  /// Set a private Property Value on a given Object. Uses Reflection. ///  /// Type of the Property /// Object from where the Property Value is returned /// Propertyname as string. /// the value to set /// if the Property is not found public static void SetPrivateFieldValue(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); } 

Puede acceder a setter privado desde el tipo derivado mediante código

 public static void SetProperty(object instance, string propertyName, object newValue) { Type type = instance.GetType(); PropertyInfo prop = type.BaseType.GetProperty(propertyName); prop.SetValue(instance, newValue, null); } 

Ninguno de estos funcionó para mí, y el nombre de mi propiedad era único, así que acabo de usar esto:

 public static void SetPrivatePropertyValue(T obj, string propertyName, object newValue) { // add a check here that the object obj and propertyName string are not null foreach (FieldInfo fi in obj.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic)) { if (fi.Name.ToLower().Contains(propertyName.ToLower())) { fi.SetValue(obj, newValue); break; } } } 
  //mock class public class Person{ public string Name{get; internal set;} } // works for all types, update private field through reflection public static T ReviveType(T t, string propertyName, object newValue){ // add a check here that the object t and propertyName string are not null PropertyInfo pi = t.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance); pi.SetValue(t, newValue, null); return t; } // check the required function void Main() { var p = new Person(){Name="John"}; Console.WriteLine("Name: {0}",p.Name); //box the person to object, just to see that the method never care about what type you pass it object o = p; var updatedPerson = ReviveType(o, "Name", "Webber") as Person; //check if it updated person instance Console.WriteLine("Name: {0}",updatedPerson.Name); } // Console Result: ------------------- Name: John Name: Webber