Skip to content

Commit 679ce24

Browse files
committed
Refactor IsSystemType
1 parent a65bd5a commit 679ce24

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

SharpHelpers/SharpHelpers/ObjectHelper.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,37 @@ namespace SharpCoding.SharpHelpers
77
{
88
public static class ObjectHelper
99
{
10+
private static readonly Type[] SystemTypes =
11+
{
12+
typeof(Enum),
13+
typeof(string),
14+
typeof(decimal),
15+
typeof(DateTime),
16+
typeof(DateTimeOffset),
17+
typeof(TimeSpan),
18+
typeof(Guid)
19+
};
20+
1021
/// <summary>
11-
/// This method verifies that the passed type is a .Net Type
22+
/// Checks if the given type is a system type (e.g., primitive, value types, common system types).
1223
/// </summary>
13-
/// <param name="type"></param>
14-
/// <returns></returns>
24+
/// <param name="type">The type to check.</param>
25+
/// <returns>True if the type is a system type; otherwise, false.</returns>
1526
public static bool IsSystemType(this Type type)
1627
{
17-
return
18-
type.IsValueType ||
19-
type.IsPrimitive ||
20-
new[]
21-
{
22-
typeof(Enum),
23-
typeof(string),
24-
typeof(decimal),
25-
typeof(DateTime),
26-
typeof(DateTimeOffset),
27-
typeof(TimeSpan),
28-
typeof(Guid)
29-
}.Contains(type) ||
30-
Convert.GetTypeCode(type) != TypeCode.Object ||
31-
(type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) &&
32-
IsSystemType(type.GetGenericArguments()[0]));
28+
if (type.IsValueType || type.IsPrimitive)
29+
return true;
30+
31+
// Handle Nullable types
32+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
33+
return IsSystemType(type.GetGenericArguments()[0]);
34+
35+
// Check against predefined system types
36+
if (SystemTypes.Contains(type))
37+
return true;
38+
39+
// Check the type code to cover other system types
40+
return Convert.GetTypeCode(type) != TypeCode.Object;
3341
}
3442
}
3543
}

0 commit comments

Comments
 (0)