From a7f8d539503cab58fc80468491829ffb5e52c684 Mon Sep 17 00:00:00 2001 From: XiaoFeiDu Date: Thu, 25 Nov 2021 15:08:58 +0800 Subject: [PATCH] Remove GetDefaultValue, add CreateInstance and IsNullableType. --- .../Zaabee.Extensions.Type.cs | 13 +-- .../Zaabee.Extensions.csproj | 4 +- .../TypeExtensionTest.cs | 90 +++++++++++++++---- .../Zaabee.Extensions.UnitTest.csproj | 2 +- 4 files changed, 81 insertions(+), 28 deletions(-) diff --git a/src/Zaabee.Extensions/Zaabee.Extensions.Type.cs b/src/Zaabee.Extensions/Zaabee.Extensions.Type.cs index e936c1d..c6bc270 100644 --- a/src/Zaabee.Extensions/Zaabee.Extensions.Type.cs +++ b/src/Zaabee.Extensions/Zaabee.Extensions.Type.cs @@ -2,14 +2,15 @@ namespace Zaabee.Extensions; public static partial class ZaabeeExtension { - private static readonly ConcurrentDictionary ValueTypeCache = new(); + public static object? CreateInstance(this Type type, params object?[]? args) => + Activator.CreateInstance(type, args); - public static object? GetDefaultValue(this Type type) => - type.IsValueType - ? ValueTypeCache.GetOrAdd(type, Activator.CreateInstance) - : null; + public static bool IsNullableType(this Type? type) => + type is not null + && type.IsGenericType + && type.GetGenericTypeDefinition() == typeof(Nullable<>); - public static bool IsNumericType(this Type? type) => + public static bool IsNumericType(this Type type) => Type.GetTypeCode(type) switch { TypeCode.Byte => true, diff --git a/src/Zaabee.Extensions/Zaabee.Extensions.csproj b/src/Zaabee.Extensions/Zaabee.Extensions.csproj index 79990e4..28c4bd5 100644 --- a/src/Zaabee.Extensions/Zaabee.Extensions.csproj +++ b/src/Zaabee.Extensions/Zaabee.Extensions.csproj @@ -3,8 +3,8 @@ netstandard2.0;netcoreapp3.1;net5.0;net6.0 true - 2021.12.0 - 2021.12.0 + 2021.13.0 + 2021.13.0 Mutuduxf Mutuduxf Zaabee;Extensions diff --git a/tests/Zaabee.Extensions.UnitTest/TypeExtensionTest.cs b/tests/Zaabee.Extensions.UnitTest/TypeExtensionTest.cs index af65de0..3915b09 100644 --- a/tests/Zaabee.Extensions.UnitTest/TypeExtensionTest.cs +++ b/tests/Zaabee.Extensions.UnitTest/TypeExtensionTest.cs @@ -3,27 +3,81 @@ namespace Zaabee.Extensions.UnitTest; public class TypeExtensionTest { [Fact] - public void TestReferenceType() + public void TestReferenceTypeCreateInstance() { - Assert.Equal(default(object), typeof(object).GetDefaultValue()); - Assert.Equal(default(TestModel), typeof(TestModel).GetDefaultValue()); + Assert.NotNull(typeof(object).CreateInstance()); + Assert.NotNull(typeof(TestModel).CreateInstance()); + } + + [Theory] + [InlineData(default(bool), typeof(bool))] + [InlineData(default(short), typeof(short))] + [InlineData(default(ushort), typeof(ushort))] + [InlineData(default(int), typeof(int))] + [InlineData(default(uint), typeof(uint))] + [InlineData(default(long), typeof(long))] + [InlineData(default(ulong), typeof(ulong))] + [InlineData(default(float), typeof(float))] + [InlineData(default(double), typeof(double))] + [InlineData(default(TestEnum), typeof(TestEnum))] + [InlineData(default(char), typeof(char))] + public void TestValueTypeCreateInstance(object? value, Type type) + { + Assert.Equal(value, type.CreateInstance()); } [Fact] - public void TestValueType() + public void TestNullableValueTypeCreateInstance() { - Assert.Equal(default(bool), typeof(bool).GetDefaultValue()); - Assert.Equal(default(short), typeof(short).GetDefaultValue()); - Assert.Equal(default(ushort), typeof(ushort).GetDefaultValue()); - Assert.Equal(default(int), typeof(int).GetDefaultValue()); - Assert.Equal(default(uint), typeof(uint).GetDefaultValue()); - Assert.Equal(default(long), typeof(long).GetDefaultValue()); - Assert.Equal(default(ulong), typeof(ulong).GetDefaultValue()); - Assert.Equal(default(float), typeof(float).GetDefaultValue()); - Assert.Equal(default(double), typeof(double).GetDefaultValue()); - Assert.Equal(default(decimal), typeof(decimal).GetDefaultValue()); - Assert.Equal(default(TestEnum), typeof(TestEnum).GetDefaultValue()); - Assert.Equal(default(char), typeof(char).GetDefaultValue()); + Assert.Equal(default(bool?), typeof(bool?).CreateInstance()); + Assert.Equal(default(short?), typeof(short?).CreateInstance()); + Assert.Equal(default(ushort?), typeof(ushort?).CreateInstance()); + Assert.Equal(default(int?), typeof(int?).CreateInstance()); + Assert.Equal(default(uint?), typeof(uint?).CreateInstance()); + Assert.Equal(default(long?), typeof(long?).CreateInstance()); + Assert.Equal(default(ulong?), typeof(ulong?).CreateInstance()); + Assert.Equal(default(float?), typeof(float?).CreateInstance()); + Assert.Equal(default(double?), typeof(double?).CreateInstance()); + Assert.Equal(default(decimal?), typeof(decimal?).CreateInstance()); + Assert.Equal(default(TestEnum?), typeof(TestEnum?).CreateInstance()); + Assert.Equal(default(char?), typeof(char?).CreateInstance()); + } + + [Theory] + [InlineData(typeof(sbyte), false)] + [InlineData(typeof(byte), false)] + [InlineData(typeof(short), false)] + [InlineData(typeof(int), false)] + [InlineData(typeof(long), false)] + [InlineData(typeof(float), false)] + [InlineData(typeof(double), false)] + [InlineData(typeof(decimal), false)] + [InlineData(typeof(ushort), false)] + [InlineData(typeof(uint), false)] + [InlineData(typeof(ulong), false)] + [InlineData(typeof(TestEnum), false)] + [InlineData(typeof(bool), false)] + [InlineData(typeof(char), false)] + [InlineData(typeof(string), false)] + [InlineData(typeof(TestModel), false)] + [InlineData(typeof(sbyte?), true)] + [InlineData(typeof(byte?), true)] + [InlineData(typeof(short?), true)] + [InlineData(typeof(int?), true)] + [InlineData(typeof(long?), true)] + [InlineData(typeof(float?), true)] + [InlineData(typeof(double?), true)] + [InlineData(typeof(decimal?), true)] + [InlineData(typeof(ushort?), true)] + [InlineData(typeof(uint?), true)] + [InlineData(typeof(ulong?), true)] + [InlineData(typeof(TestEnum?), true)] + [InlineData(typeof(bool?), true)] + [InlineData(typeof(char?), true)] + [InlineData(null, false)] + public void IsNullableTypeTest(Type? type, bool result) + { + Assert.Equal(type.IsNullableType(), result); } [Theory] @@ -43,11 +97,9 @@ public void TestValueType() [InlineData(typeof(char), false)] [InlineData(typeof(string), false)] [InlineData(typeof(TestModel), false)] + [InlineData(null, false)] public void IsNumericTypeTest(Type type, bool result) { Assert.Equal(type.IsNumericType(), result); - - Type? nullType = null; - Assert.False(nullType.IsNumericType()); } } \ No newline at end of file diff --git a/tests/Zaabee.Extensions.UnitTest/Zaabee.Extensions.UnitTest.csproj b/tests/Zaabee.Extensions.UnitTest/Zaabee.Extensions.UnitTest.csproj index 15a0c73..a9e3028 100644 --- a/tests/Zaabee.Extensions.UnitTest/Zaabee.Extensions.UnitTest.csproj +++ b/tests/Zaabee.Extensions.UnitTest/Zaabee.Extensions.UnitTest.csproj @@ -3,8 +3,8 @@ net48;netcoreapp3.1;net5.0;net6.0 false - enable latest + enable