C # Setting an object to DateTime property values ​​through reflection

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?

+7
source share
1 answer

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.

 p.SetValue(this, dt, null); 
+9
source

All Articles