I want to set all DateTime properties of my object to the default date. However, if I try to set the values ​​through reflection, I get an exception: "The object does not match the type of target."
private void SetDefaultValues() { DateTime dt = DateTime.Parse("1/1/2000", new CultureInfo("en-US", true)); foreach (PropertyInfo p in this.GetType().GetProperties()) { if (p.PropertyType.FullName == "System.DateTime") { p.SetValue(dt, typeof(DateTime), null); } } }
Am I doing / thinking something fundamentally wrong?
Parameters must be configured; the first is the goal I assume this here; the second is the value ( dt ). The latter refers to “indexers,” which are probably not used here.
this
dt
p.SetValue(this, dt, null);