Skip to content

Simplifying TypeExtensions.cs IsCustomType method #31

Open
@Tihomirov-Vasiliy

Description

@Tihomirov-Vasiliy

Hi, I think the code in TypeExtensions.cs

public static bool IsCustomType(this Type? type)
{
    var builtInTypes = new[]
    {
            typeof(string),
            typeof(decimal),
            typeof(DateTime),
            typeof(DateTimeOffset),
            typeof(TimeSpan),
            typeof(Guid)
    };

    return type != null && type.IsClass && !type.IsEnum && !type.IsValueType && !type.IsPrimitive && !builtInTypes.Contains(type);
}

can be simplified by changing the method to:

public static bool IsCustomType(this Type? type)
{
    return type != null && type.IsClass
        && !type.IsEnum && !type.IsValueType
        && !type.IsPrimitive && type != typeof(string);
}

you can check it just with unit-test below (i was using NUnit):

[TestCase(typeof(DateTime))]
[TestCase(typeof(DateTimeOffset))]
[TestCase(typeof(TimeSpan))]
[TestCase(typeof(Guid))]
[TestCase(typeof(decimal))]
[TestCase(typeof(string))]
[TestCase(null)]
public void TypeIsCustom(Type? type)
{
    Assert.IsFalse(
        type != null && type.IsClass 
        && !type.IsEnum && !type.IsValueType 
        && !type.IsPrimitive && type != typeof(string));
}

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions