I want to set all DateTime properties of my object to a default date. However, if I try do set the values through reflection I get the exception: "Object does not match target type."
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 incorrect?
Parameters need adjusting; the first is the target - which I assume is this
here; the second is the value (dt
). The last relates to "indexers" - which probably doesn't apply here.
p.SetValue(this, dt, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With