Skip to content

Commit defc555

Browse files
committed
Removed some unnecessary code
1 parent 93585f5 commit defc555

File tree

1 file changed

+0
-266
lines changed

1 file changed

+0
-266
lines changed

tests/IGLib.Core.Tests/TypesTests/NonGenericConversions/UtilTypesSingle.cs

Lines changed: 0 additions & 266 deletions
Original file line numberDiff line numberDiff line change
@@ -195,272 +195,6 @@ public static int ToInt(this object? value, bool precise = false, IFormatProvide
195195

196196

197197

198-
199-
//#region GenericConversionOfBaseTypes
200-
201-
////public static object? ConvertToType2222(this object? value, Type targetType, bool precise = false, IFormatProvider? provider = null)
202-
////{
203-
//// throw new NotImplementedException();
204-
////}
205-
206-
////public static object? ConvertTo2222<TargetType>(this object? value, bool precise = false, IFormatProvider? provider = null)
207-
//// where TargetType : IConvertible
208-
////{
209-
//// return ConvertToType2222(value, typeof(TargetType), precise, provider);
210-
////}
211-
212-
213-
214-
///// <summary>
215-
///// 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>
235-
//public static object? ConvertToType(this object? value, Type targetType, bool precise = false, IFormatProvider? provider = null)
236-
//{
237-
// if (targetType == null)
238-
// throw new ArgumentNullException(nameof(targetType));
239-
240-
// provider ??= CultureInfo.InvariantCulture;
241-
242-
// // Handle nullable target types
243-
// Type? underlying = Nullable.GetUnderlyingType(targetType);
244-
// bool targetIsNullable = underlying != null;
245-
// 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
349-
// try
350-
// {
351-
// return Convert.ChangeType(s, effectiveTarget, provider)!;
352-
// }
353-
// catch (Exception ex)
354-
// {
355-
// throw new InvalidOperationException($"{nameof(UtilTypesSingle)}.{nameof(ConvertToType)}: Cannot convert string '{s}' to {effectiveTarget.Name}.", ex);
356-
// }
357-
// }
358-
359-
// // If value implements IConvertible => general conversion via Convert.ChangeType
360-
// if (value is IConvertible)
361-
// {
362-
// try
363-
// {
364-
// object converted = Convert.ChangeType(value, effectiveTarget, provider)!;
365-
366-
// if (!precise)
367-
// return converted;
368-
369-
// // Precise mode: attempt round-trip to ensure no information loss
370-
// try
371-
// {
372-
// object roundTrip = Convert.ChangeType(converted, sourceType, provider)!;
373-
// if (Equals(value, roundTrip))
374-
// return converted;
375-
376-
// 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"/>.
396-
///// </summary>
397-
//public static TargetType? ConvertTo<TargetType>(this object? value, bool precise = false, IFormatProvider? provider = null)
398-
// where TargetType : IConvertible
399-
//{
400-
// object? result = ConvertToType(value, typeof(TargetType), precise, provider);
401-
// if (result is null) return default;
402-
// return (TargetType)result;
403-
//}
404-
405-
406-
///// <summary>Returns true if <paramref name="type"/> is a numerical type (supporting arythmetic
407-
///// operations), false otherwise.</summary>
408-
///// <param name="type">Type for which th query is performed.</param>
409-
//private static bool IsNumericType(Type type)
410-
//{
411-
// return type == typeof(byte) || type == typeof(sbyte)
412-
// || type == typeof(short) || type == typeof(ushort)
413-
// || type == typeof(int) || type == typeof(uint)
414-
// || type == typeof(long) || type == typeof(ulong)
415-
// || type == typeof(float) || type == typeof(double)
416-
// || type == typeof(decimal);
417-
//}
418-
419-
420-
//#endregion GenericConversionOfBaseTypes
421-
422-
423-
424-
425-
///// <summary>Returns true if all members of the specified collection (<paramref name="collection"/>)
426-
///// are of the specified primitive (unmanaged, most commonly numeric) type (<typeparamref name="ConvertedType"/>).
427-
///// <para>For example, if the collection elements are of declared type object and the first type parameter
428-
///// is <see cref="int"/> then true is returned if and only if all elements of the collection are actually
429-
///// of type int and are NOT null.</para></summary>
430-
///// <typeparam name="ConvertedType">The type for which elements of the collection are checked. It must
431-
///// be an unmanaged type such as bool, int, char, byte, long, float, double, etc.</typeparam>
432-
///// <typeparam name="CollectionElementType">Declared type of collection elements.</typeparam>
433-
///// <param name="collection">The collection whose elements are checked for whether they are of a specified type.</param>
434-
///// <returns>True if all elements are of the specified type and are not null, false otherwise.</returns>
435-
//public static bool IsConvertibleToCollectionOf<ConvertedType, CollectionElementType>(
436-
// IEnumerable<CollectionElementType>? collection)
437-
// where ConvertedType : IConvertible
438-
//{
439-
// if (collection == null)
440-
// {
441-
// return true;
442-
// }
443-
// foreach (CollectionElementType item in collection)
444-
// {
445-
// if (item == null)
446-
// {
447-
// return false; // if number types, they cannot be null
448-
// }
449-
// if (item is not ConvertedType)
450-
// {
451-
// return false;
452-
// }
453-
// }
454-
// return true;
455-
//}
456-
457-
//public static void Test1()
458-
//{
459-
// object[] ObjectArrayOfIntDouble = [1, 2, 3, 1.11, 2.22];
460-
// // bool isIntCollection = IsIntCollectionOf(ObjectArrayOfIntDouble);
461-
// bool isIntCollection1 = IsNumberCollection<int, object>(ObjectArrayOfIntDouble);
462-
//}
463-
464198
}
465199

466200
}

0 commit comments

Comments
 (0)