1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Tango.Telemetry.Helpers
{
internal static class DateTimeUtcFixer
{
/// <summary>
/// Recursively scans the object graph starting at <paramref name="obj"/> and
/// sets Kind=Utc on any DateTime/DateTime? properties or collection elements that are Kind=Unspecified.
/// Uses DateTime.SpecifyKind(value, DateTimeKind.Utc) (no clock adjustment).
/// Notes:
/// - Only properties are updated (not fields).
/// - Collections: updates elements for IList/arrays and dictionary values; traverses other IEnumerable to reach nested objects.
/// - Read-only properties (no setter) are skipped.
/// - Root objects that are a DateTime value cannot be changed in place (struct); wrap them in a container if needed.
/// </summary>
public static void EnsureDateTimeUTC(object obj)
{
var visited = new HashSet<object>(ReferenceEqualityComparer.Instance);
EnsureUtcInternal(obj, visited);
}
private static void EnsureUtcInternal(object obj, HashSet<object> visited)
{
if (obj == null) return;
var type = obj.GetType();
// Avoid revisiting reference objects (handle cycles, EF proxies, etc.)
if (!type.IsValueType) // only track reference types
{
if (!visited.Add(obj)) return;
}
// Handle dictionaries: update values, recurse into objects
if (obj is IDictionary dict)
{
var keys = new List<object>();
foreach (var k in dict.Keys) keys.Add(k);
foreach (var key in keys)
{
var value = dict[key];
if (TrySpecifyUtcOnBoxedDateTime(ref value))
{
dict[key] = value;
}
else
{
EnsureUtcInternal(value, visited);
}
}
return;
}
// Handle lists/arrays: update elements, recurse into objects
if (obj is IList list)
{
for (int i = 0; i < list.Count; i++)
{
var element = list[i];
if (TrySpecifyUtcOnBoxedDateTime(ref element))
{
list[i] = element;
}
else
{
EnsureUtcInternal(element, visited);
}
}
return;
}
// Traverse other enumerables just to reach nested objects (cannot replace DateTime elements)
if (obj is IEnumerable enumerable && !(obj is string))
{
foreach (var element in enumerable)
{
EnsureUtcInternal(element, visited);
}
}
// Inspect properties
const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
foreach (var prop in type.GetProperties(flags))
{
// indexers not supported
if (prop.GetIndexParameters().Length != 0) continue;
// must be readable
if (!prop.CanRead) continue;
var pType = prop.PropertyType;
// DateTime
if (pType == typeof(DateTime))
{
var val = (DateTime)prop.GetValue(obj);
if (val.Kind == DateTimeKind.Unspecified)
{
var newVal = DateTime.SpecifyKind(val, DateTimeKind.Utc);
TrySetProperty(obj, prop, newVal);
}
continue;
}
// Nullable<DateTime>
var underlying = Nullable.GetUnderlyingType(pType);
if (underlying == typeof(DateTime))
{
var val = (DateTime?)prop.GetValue(obj);
if (val.HasValue && val.Value.Kind == DateTimeKind.Unspecified)
{
var newVal = (DateTime?)DateTime.SpecifyKind(val.Value, DateTimeKind.Utc);
TrySetProperty(obj, prop, newVal);
}
continue;
}
// Skip obvious simple types
if (IsSimple(pType)) continue;
// Recurse into complex objects
var child = prop.GetValue(obj);
EnsureUtcInternal(child, visited);
}
}
private static bool TrySpecifyUtcOnBoxedDateTime(ref object obj)
{
if (obj == null) return false;
var t = obj.GetType();
// Exact DateTime
if (t == typeof(DateTime))
{
var dt = (DateTime)obj;
if (dt.Kind == DateTimeKind.Unspecified)
{
obj = DateTime.SpecifyKind(dt, DateTimeKind.Utc);
}
return true;
}
// Nullable<DateTime> boxed
var underlying = Nullable.GetUnderlyingType(t);
if (underlying == typeof(DateTime))
{
// boxed Nullable<DateTime> can be unboxed to DateTime? safely
var ndt = (DateTime?)obj;
if (ndt.HasValue && ndt.Value.Kind == DateTimeKind.Unspecified)
{
obj = (DateTime?)DateTime.SpecifyKind(ndt.Value, DateTimeKind.Utc);
}
return true;
}
return false;
}
private static bool TrySetProperty(object target, PropertyInfo prop, object value)
{
try
{
// Prefer invoking the setter (even if non-public)
var setter = prop.GetSetMethod(nonPublic: true);
if (setter == null) return false;
setter.Invoke(target, new[] { value });
return true;
}
catch
{
// Fall back to PropertyInfo.SetValue (may still work in some runtimes)
try
{
prop.SetValue(target, value);
return true;
}
catch
{
return false;
}
}
}
private static bool IsSimple(Type t)
{
if (t.IsPrimitive || t.IsEnum) return true;
if (t == typeof(string) || t == typeof(decimal) || t == typeof(Guid) ||
t == typeof(Uri) || t == typeof(TimeSpan) || t == typeof(DateTimeOffset))
return true;
// Value types other than DateTime/Nullable<DateTime> are considered simple
return t.IsValueType;
}
/// <summary>Reference equality comparer for the visited set.</summary>
private sealed class ReferenceEqualityComparer : IEqualityComparer<object>
{
public static readonly ReferenceEqualityComparer Instance = new ReferenceEqualityComparer();
public new bool Equals(object x, object y) => ReferenceEquals(x, y);
public int GetHashCode(object obj) => RuntimeHelpers.GetHashCode(obj);
}
}
}
|