Skip to content

Commit 0d535d2

Browse files
authored
feat(Core): add BootstrapBlazor.Core project (#4205)
* chore: 增加 Core 工程 * chore: bump version 8.9.1-beta01 * chore: 更新 readme 文件 * test: 增加单元测试 * test: 更新单元测试 * test: 更新单元测试
1 parent ccfa790 commit 0d535d2

File tree

11 files changed

+181
-38
lines changed

11 files changed

+181
-38
lines changed

BootstrapBlazor.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.WinBox", "s
162162
EndProject
163163
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BootstrapBlazor.Player", "src\Extensions\Components\BootstrapBlazor.Player\BootstrapBlazor.Player.csproj", "{C6145F86-D8F0-42A6-AE06-0EABA90ECD6B}"
164164
EndProject
165+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.Core", "src\BootstrapBlazor.Core\BootstrapBlazor.Core.csproj", "{6162D045-845C-4753-9A4D-52090D9390D5}"
166+
EndProject
165167
Global
166168
GlobalSection(SolutionConfigurationPlatforms) = preSolution
167169
Debug|Any CPU = Debug|Any CPU
@@ -382,6 +384,10 @@ Global
382384
{C6145F86-D8F0-42A6-AE06-0EABA90ECD6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
383385
{C6145F86-D8F0-42A6-AE06-0EABA90ECD6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
384386
{C6145F86-D8F0-42A6-AE06-0EABA90ECD6B}.Release|Any CPU.Build.0 = Release|Any CPU
387+
{6162D045-845C-4753-9A4D-52090D9390D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
388+
{6162D045-845C-4753-9A4D-52090D9390D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
389+
{6162D045-845C-4753-9A4D-52090D9390D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
390+
{6162D045-845C-4753-9A4D-52090D9390D5}.Release|Any CPU.Build.0 = Release|Any CPU
385391
EndGlobalSection
386392
GlobalSection(SolutionProperties) = preSolution
387393
HideSolutionNode = FALSE
@@ -448,6 +454,7 @@ Global
448454
{50F286FD-D814-46C3-A70C-C2F789C4338C} = {CD062AB6-244D-402A-8F33-C37DAC5856CC}
449455
{C19A57F2-DBE3-4CD4-96EA-337C93A4D886} = {CD062AB6-244D-402A-8F33-C37DAC5856CC}
450456
{C6145F86-D8F0-42A6-AE06-0EABA90ECD6B} = {CD062AB6-244D-402A-8F33-C37DAC5856CC}
457+
{6162D045-845C-4753-9A4D-52090D9390D5} = {A2182155-43ED-44C1-BF6F-1B70EBD2DFFE}
451458
EndGlobalSection
452459
GlobalSection(ExtensibilityGlobals) = postSolution
453460
SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<Version>8.0.0</Version>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Using Include="BootstrapBlazor.Components" />
9+
</ItemGroup>
10+
11+
</Project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.Text.Json;
6+
using System.Text.Json.Serialization;
7+
8+
namespace BootstrapBlazor.Core.Converter;
9+
10+
/// <summary>
11+
/// 枚举类型转换器
12+
/// </summary>
13+
/// <remarks>序列化时把枚举类型序列化成字符串</remarks>
14+
public class JsonDescriptionEnumConverter<T> : JsonConverter<T> where T : struct, Enum
15+
{
16+
/// <summary>
17+
/// <inheritdoc/>
18+
/// </summary>
19+
/// <param name="reader"></param>
20+
/// <param name="typeToConvert"></param>
21+
/// <param name="options"></param>
22+
/// <returns></returns>
23+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
24+
{
25+
T ret = default;
26+
if (reader.TokenType == JsonTokenType.String)
27+
{
28+
var enumStringValue = reader.GetString();
29+
var v = Enum.GetNames<T>().FirstOrDefault(i => typeof(T).ToDescriptionString(i) == enumStringValue);
30+
if (Enum.TryParse<T>(v, true, out T val))
31+
{
32+
ret = val;
33+
}
34+
}
35+
return ret;
36+
}
37+
38+
/// <summary>
39+
/// <inheritdoc/>
40+
/// </summary>
41+
/// <param name="writer"></param>
42+
/// <param name="value"></param>
43+
/// <param name="options"></param>
44+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
45+
{
46+
writer.WriteStringValue(value.ToDescriptionString());
47+
}
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project>
2+
3+
<Import Project="..\Directory.Build.props" />
4+
<Import Project="..\Frameworks.props" />
5+
<Import Project="..\Logo.props" />
6+
<Import Project="..\SourceLink.targets" />
7+
8+
<PropertyGroup>
9+
<PackageTags>Bootstrap Blazor WebAssembly wasm UI Components</PackageTags>
10+
<Description>Bootstrap UI components for Blazor and Razor Components</Description>
11+
<PackageReadmeFile>readme.md</PackageReadmeFile>
12+
<PackageReleaseNotes>https://github.com/dotnetcore/BootstrapBlazor/releases?wt.mc_id=DT-MVP-5004174</PackageReleaseNotes>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<None Include="readme.md" Pack="true" PackagePath="" />
17+
</ItemGroup>
18+
19+
<PropertyGroup>
20+
<NoWarn>NU1902;NU1903;CS8002</NoWarn>
21+
</PropertyGroup>
22+
23+
</Project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
using System.ComponentModel;
6+
using System.Reflection;
7+
8+
namespace BootstrapBlazor.Components;
9+
10+
/// <summary>
11+
/// 枚举类型扩展方法
12+
/// </summary>
13+
public static class EnumExtensions
14+
{
15+
/// <summary>
16+
/// 获取 DescriptionAttribute 标签方法
17+
/// </summary>
18+
/// <param name="val"></param>
19+
/// <returns></returns>
20+
public static string ToDescriptionString<TEnum>(this TEnum val) where TEnum : Enum => typeof(TEnum).ToDescriptionString(val.ToString());
21+
22+
/// <summary>
23+
/// 通过字段名称获取 DescriptionAttribute 标签值
24+
/// </summary>
25+
/// <param name="type"></param>
26+
/// <param name="fieldName"></param>
27+
/// <returns></returns>
28+
public static string ToDescriptionString(this Type? type, string? fieldName)
29+
{
30+
var ret = string.Empty;
31+
if (type != null && !string.IsNullOrEmpty(fieldName))
32+
{
33+
var t = Nullable.GetUnderlyingType(type) ?? type;
34+
var attributes = t.GetField(fieldName)?.GetCustomAttribute<DescriptionAttribute>(true);
35+
ret = attributes?.Description ?? fieldName;
36+
}
37+
return ret;
38+
}
39+
}

src/BootstrapBlazor.Core/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Bootstrap Blazor Core Library
2+
3+
## Features
4+
- Provides a series of extension methods

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>8.9.0</Version>
4+
<Version>8.9.1-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
@@ -63,6 +63,10 @@
6363
<None Include="wwwroot\src\**\*.*" />
6464
</ItemGroup>
6565

66+
<ItemGroup>
67+
<ProjectReference Include="..\BootstrapBlazor.Core\BootstrapBlazor.Core.csproj" />
68+
</ItemGroup>
69+
6670
<ItemGroup>
6771
<Using Include="BootstrapBlazor.Components" />
6872
<Using Include="Microsoft.AspNetCore.Components" />

src/BootstrapBlazor/Extensions/EnumExtensions.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,6 @@ namespace BootstrapBlazor.Components;
1212
/// </summary>
1313
public static class EnumExtensions
1414
{
15-
/// <summary>
16-
/// 获取 DescriptionAttribute 标签方法
17-
/// </summary>
18-
/// <param name="val"></param>
19-
/// <returns></returns>
20-
public static string ToDescriptionString<TEnum>(this TEnum val) where TEnum : Enum => typeof(TEnum).ToDescriptionString(val.ToString());
21-
22-
/// <summary>
23-
/// 通过字段名称获取 DescriptionAttribute 标签值
24-
/// </summary>
25-
/// <param name="type"></param>
26-
/// <param name="fieldName"></param>
27-
/// <returns></returns>
28-
public static string ToDescriptionString(this Type? type, string? fieldName)
29-
{
30-
var ret = string.Empty;
31-
if (type != null && !string.IsNullOrEmpty(fieldName))
32-
{
33-
var t = Nullable.GetUnderlyingType(type) ?? type;
34-
var attributes = t.GetField(fieldName)?.GetCustomAttribute<DescriptionAttribute>(true);
35-
ret = attributes?.Description ?? fieldName;
36-
}
37-
return ret;
38-
}
39-
4015
/// <summary>
4116
/// 通过字段名称获取 DisplayAttribute/DescriptionAttribute 标签值
4217
/// </summary>

src/Extensions/Components/BootstrapBlazor.Core/BootstrapBlazor.Core.csproj

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)