You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
///// Converts the specified <paramref name="value"/> to the specified <paramref name="targetType"/>.
216
-
///// Supports <see cref="Nullable{T}"/> target types. For string inputs, type-safe <c>TryParse</c> paths are used for well-known primitive types to avoid reflection.
217
-
///// Uses <see cref="Convert.ChangeType(object, Type, IFormatProvider?)"/> as a fallback for other IConvertible types.
218
-
///// </summary>
219
-
///// <param name="value">The value to convert. May be <see langword="null"/> if the target type is nullable.</param>
220
-
///// <param name="targetType">The target <see cref="Type"/>. Must implement <see cref="IConvertible"/> (or be a nullable of a type that does).</param>
221
-
///// <param name="precise">
222
-
///// When <see langword="true"/>, ensures that numeric conversions are lossless (no fractional/truncation or other information loss).
223
-
///// When <see langword="false"/>, lossy conversions are allowed. Default is <see langword="false"/>.
224
-
///// </param>
225
-
///// <param name="provider">
226
-
///// Optional <see cref="IFormatProvider"/> (for example, <see cref="CultureInfo.InvariantCulture"/>).
227
-
///// If <see langword="null"/>, <see cref="CultureInfo.InvariantCulture"/> is used.
228
-
///// </param>
229
-
///// <returns>
230
-
///// The converted value boxed as <see cref="object"/> (or <see langword="null"/> when the target is nullable and <paramref name="value"/> was <see langword="null"/> or an empty/whitespace string).
231
-
///// </returns>
232
-
///// <exception cref="ArgumentNullException">Thrown when <paramref name="targetType"/> is <see langword="null"/>.</exception>
233
-
///// <exception cref="InvalidOperationException">Thrown if <paramref name="targetType"/> does not implement <see cref="IConvertible"/>, or conversion is not possible (or not precise when <paramref name="precise"/> is true).</exception>
234
-
///// <exception cref="FormatException">Thrown when parsing a string fails for known types (int, double, etc.).</exception>
// Type effectiveTarget = underlying ?? targetType;
246
-
247
-
// // If source is null: return null for nullable target, otherwise fail
248
-
// if (value is null)
249
-
// return targetIsNullable ? null :
250
-
// throw new InvalidOperationException($"{nameof(UtilTypesSingle)}.{nameof(ConvertToType)
251
-
// }: Cannot convert null to non-nullable target type {targetType.Name}.");
252
-
253
-
// // If effective target doesn't implement IConvertible -> fail
254
-
// if (!typeof(IConvertible).IsAssignableFrom(effectiveTarget))
255
-
// throw new InvalidOperationException($"{nameof(UtilTypesSingle)}.{nameof(ConvertToType)}: Target type {effectiveTarget.Name} does not implement IConvertible.");
256
-
257
-
// Type sourceType = value.GetType();
258
-
259
-
// // If the value already matches the target (assignable), return it (handles reference types)
260
-
// if (effectiveTarget.IsAssignableFrom(sourceType))
261
-
// return value;
262
-
263
-
// // If value is string -> use explicit TryParse for known types (no reflection)
264
-
// if (value is string s)
265
-
// {
266
-
// // treat empty/whitespace as null for nullable target types
267
-
// if (string.IsNullOrWhiteSpace(s) && targetIsNullable)
268
-
// return null;
269
-
270
-
// if (effectiveTarget == typeof(int))
271
-
// {
272
-
// if (int.TryParse(s, NumberStyles.Integer, provider, out var i)) return i;
273
-
// throw new FormatException($"Cannot parse '{s}' as int.");
274
-
// }
275
-
276
-
// if (effectiveTarget == typeof(long))
277
-
// {
278
-
// if (long.TryParse(s, NumberStyles.Integer, provider, out var l)) return l;
279
-
// throw new FormatException($"Cannot parse '{s}' as long.");
280
-
// }
281
-
282
-
// if (effectiveTarget == typeof(short))
283
-
// {
284
-
// if (short.TryParse(s, NumberStyles.Integer, provider, out var sh)) return sh;
285
-
// throw new FormatException($"Cannot parse '{s}' as short.");
286
-
// }
287
-
288
-
// if (effectiveTarget == typeof(byte))
289
-
// {
290
-
// if (byte.TryParse(s, NumberStyles.Integer, provider, out var by)) return by;
291
-
// throw new FormatException($"Cannot parse '{s}' as byte.");
292
-
// }
293
-
294
-
// if (effectiveTarget == typeof(uint))
295
-
// {
296
-
// if (uint.TryParse(s, NumberStyles.Integer, provider, out var ui)) return ui;
297
-
// throw new FormatException($"Cannot parse '{s}' as uint.");
298
-
// }
299
-
300
-
// if (effectiveTarget == typeof(ulong))
301
-
// {
302
-
// if (ulong.TryParse(s, NumberStyles.Integer, provider, out var ul)) return ul;
303
-
// throw new FormatException($"Cannot parse '{s}' as ulong.");
304
-
// }
305
-
306
-
// if (effectiveTarget == typeof(float))
307
-
// {
308
-
// if (float.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider, out var f)) return f;
309
-
// throw new FormatException($"Cannot parse '{s}' as float.");
310
-
// }
311
-
312
-
// if (effectiveTarget == typeof(double))
313
-
// {
314
-
// if (double.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, provider, out var d)) return d;
315
-
// throw new FormatException($"Cannot parse '{s}' as double.");
316
-
// }
317
-
318
-
// if (effectiveTarget == typeof(decimal))
319
-
// {
320
-
// if (decimal.TryParse(s, NumberStyles.Number, provider, out var dec)) return dec;
321
-
// throw new FormatException($"Cannot parse '{s}' as decimal.");
322
-
// }
323
-
324
-
// if (effectiveTarget == typeof(bool))
325
-
// {
326
-
// if (bool.TryParse(s, out var b)) return b;
327
-
// throw new FormatException($"Cannot parse '{s}' as bool.");
328
-
// }
329
-
330
-
// if (effectiveTarget == typeof(DateTime))
331
-
// {
332
-
// if (DateTime.TryParse(s, provider, DateTimeStyles.None, out var dt)) return dt;
333
-
// throw new FormatException($"Cannot parse '{s}' as DateTime.");
334
-
// }
335
-
336
-
// if (effectiveTarget == typeof(Guid))
337
-
// {
338
-
// if (Guid.TryParse(s, out var g)) return g;
339
-
// throw new FormatException($"Cannot parse '{s}' as Guid.");
340
-
// }
341
-
342
-
// if (effectiveTarget == typeof(char))
343
-
// {
344
-
// if (char.TryParse(s, out var ch)) return ch;
345
-
// throw new FormatException($"Cannot parse '{s}' as Char.");
346
-
// }
347
-
348
-
// // Fallback for other IConvertible types: use Convert.ChangeType
// throw new InvalidOperationException($"{nameof(UtilTypesSingle)}.{nameof(ConvertToType)}: Conversion from {sourceType.Name} to {effectiveTarget.Name} loses precision.");
377
-
// }
378
-
// catch (InvalidCastException)
379
-
// {
380
-
// // If round-trip cannot be performed because sourceType is not convertible back, consider it not precise
381
-
// throw new InvalidOperationException($"{nameof(UtilTypesSingle)}.{nameof(ConvertToType)}: Conversion to {effectiveTarget.Name} cannot be verified as precise (round-trip failed).");
382
-
// }
383
-
// }
384
-
// catch (Exception ex)
385
-
// {
386
-
// throw new InvalidOperationException($"{nameof(UtilTypesSingle)}.{nameof(ConvertToType)}: Cannot convert {sourceType.Name} to {effectiveTarget.Name}.", ex);
387
-
// }
388
-
// }
389
-
390
-
// // Not convertible
391
-
// throw new InvalidOperationException($"{nameof(UtilTypesSingle)}.{nameof(ConvertToType)}: Value of type {sourceType.Name} cannot be converted to {effectiveTarget.Name}.");
392
-
//}
393
-
394
-
///// <summary>
395
-
///// Generic wrapper that calls <see cref="ConvertToType(object?, Type, bool, IFormatProvider?)"/> and casts the result to <typeparamref name="TargetType"/>.
0 commit comments