Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplifying TypeExtensions.cs IsCustomType method #31

Open
Tihomirov-Vasiliy opened this issue Dec 23, 2023 · 2 comments · May be fixed by #32
Open

Simplifying TypeExtensions.cs IsCustomType method #31

Tihomirov-Vasiliy opened this issue Dec 23, 2023 · 2 comments · May be fixed by #32
Labels
enhancement New feature or request
Milestone

Comments

@Tihomirov-Vasiliy
Copy link

Tihomirov-Vasiliy commented Dec 23, 2023

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

@mvdgun mvdgun added the enhancement New feature or request label Dec 24, 2023
@mvdgun mvdgun added this to the v1.5 milestone Dec 24, 2023
@mvdgun
Copy link
Member

mvdgun commented Dec 24, 2023

@Tihomirov-Vasiliy interesting, I just added some tests to test the IsCustomType extension method. Feel free to open a PR!

@Tihomirov-Vasiliy
Copy link
Author

@mvdgun I've just created the pull request #32, feel free to review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants