How To Set DateTime.Kind for all DateTime Properties on an Object using Reflection

Works fine when I try it. Beware that you are only changing the Kind, not the time. And you don't handle null dates properly, you cannot use date.Value if date.HasValue is false. Make sure that the exception isn't caught silently and bypassing the rest of the property assignments. Fix:

            // Same check for nullable DateTime.
            else if (p.PropertyType == typeof(Nullable<DateTime>)) {
                DateTime? date = (DateTime?)p.GetValue(obj, null);
                if (date.HasValue) {
                    DateTime? newDate = DateTime.SpecifyKind(date.Value, DateTimeKind.Utc);
                    p.SetValue(obj, newDate, null);
                }
            }