Is it possible to set private property via reflection?
.net, private, properties, reflection
Solution
t.GetProperty("CreatedOn")
.SetValue(obj, new DateTime(2009, 10, 14), null);
EDIT: Since the property itself is public, you apparently don't need to use `BindingFlags.NonPublic` to find it. Calling `SetValue` despite the the setter having less accessibility still does what you expect.
Problem
Can I set a private property via reflection? ``` 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); } } } ``` I've tried the following and it does not work, where `t` represents a type of `Entity`: ``` var t = typeof(Entity); var mi = t.GetMethod("set_CreatedOn", BindingFlags.Instance | BindingFlags.NonPublic); ``` I guess I can do this but I can't work it out.