Skip to content

Commit

Permalink
Remove GetDefaultValue, add CreateInstance and IsNullableType.
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoFeiDu committed Nov 25, 2021
1 parent b8c7f3f commit a7f8d53
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 28 deletions.
13 changes: 7 additions & 6 deletions src/Zaabee.Extensions/Zaabee.Extensions.Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ namespace Zaabee.Extensions;

public static partial class ZaabeeExtension
{
private static readonly ConcurrentDictionary<Type, object?> 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,
Expand Down
4 changes: 2 additions & 2 deletions src/Zaabee.Extensions/Zaabee.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2021.12.0</PackageVersion>
<Version>2021.12.0</Version>
<PackageVersion>2021.13.0</PackageVersion>
<Version>2021.13.0</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageTags>Zaabee;Extensions</PackageTags>
Expand Down
90 changes: 71 additions & 19 deletions tests/Zaabee.Extensions.UnitTest/TypeExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>net48;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit a7f8d53

Please sign in to comment.