Skip to content

Commit cbb46c3

Browse files
authored
Merge pull request #322 from Senparc/Develop
v3.1.2 add ValidationHelper.CheckNull
2 parents 2c4676d + 4959b3d commit cbb46c3

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#region Apache License Version 2.0
2+
/*----------------------------------------------------------------
3+
4+
Copyright 2025 Suzhou Senparc Network Technology Co.,Ltd.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
7+
except in compliance with the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software distributed under the
12+
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
either express or implied. See the License for the specific language governing permissions
14+
and limitations under the License.
15+
16+
Detail: https://github.com/Senparc/Senparc.CO2NET/blob/master/LICENSE
17+
18+
----------------------------------------------------------------*/
19+
#endregion Apache License Version 2.0
20+
21+
/*----------------------------------------------------------------
22+
Copyright (C) 2025 Senparc
23+
24+
FileName:ValidationHelper.cs
25+
File Function Description:Validation Helper
26+
27+
28+
Creation Identifier:Senparc - 20250128
29+
30+
----------------------------------------------------------------*/
31+
32+
33+
using Senparc.CO2NET.Exceptions;
34+
using Senparc.CO2NET.Trace;
35+
using System;
36+
using System.Collections.Generic;
37+
using System.Linq;
38+
using System.Runtime.CompilerServices;
39+
using System.Text;
40+
using System.Threading.Tasks;
41+
42+
namespace Senparc.CO2NET.Helpers.Validations
43+
{
44+
/// <summary>
45+
/// 验证帮助类
46+
/// </summary>
47+
public static class ValidationHelper
48+
{
49+
/// <summary>
50+
/// 检查对象是否为 null,如果是 null,则抛出异常并包含调用者信息。
51+
/// </summary>
52+
/// <param name="obj">要检查的对象</param>
53+
/// <param name="memberName">调用者的成员名称(自动填充)</param>
54+
/// <param name="filePath">调用者的文件路径(自动填充)</param>
55+
/// <param name="lineNumber">调用者的行号(自动填充)</param>
56+
/// <param name="throwException">是否抛出异常</param>
57+
/// <exception cref="ArgumentNullException">当对象为 null 时抛出</exception>
58+
public static void CheckNull(this object obj,
59+
[CallerMemberName] string memberName = "",
60+
[CallerFilePath] string filePath = "",
61+
[CallerLineNumber] int lineNumber = 0, bool throwException = false)
62+
{
63+
if (obj == null)
64+
{
65+
var msg = $"Parameter '{nameof(obj)}' is null. " +
66+
$"Called from '{memberName}' in '{filePath}' at line {lineNumber}.";
67+
var ex = new SenparcNullReferenceException(msg);
68+
SenparcTrace.BaseExceptionLog(ex);
69+
if (throwException)
70+
{
71+
throw ex;
72+
}
73+
}
74+
}
75+
}
76+
}

src/Senparc.CO2NET/Senparc.CO2NET.csproj

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>net462;netstandard2.0;netstandard2.1</TargetFrameworks>
4-
<Version>3.1.1</Version>
4+
<Version>3.1.2</Version>
55
<LangVersion>10.0</LangVersion>
66
<AssemblyName>Senparc.CO2NET</AssemblyName>
77
<RootNamespace>Senparc.CO2NET</RootNamespace>
@@ -134,18 +134,19 @@
134134
v2.3.0 Adapt to .NET 8.0
135135
v2.3.1 Add app.UseSenparcGlobal extension method
136136
v2.4.0 Add SenparcDI.GetService&lt;T&gt;(), SenparcDI.GetRequiredService&lt;T&gt;(), SenparcDI.GetRequreidKeyedService() methods
137-
v2.4.1 Add refresh parameter to SenparcDI.GetServiceProvider() method
138-
v2.4.2 Add ReflectionHelper.HasParameterlessConstructor() method
139-
v2.4.3 Update DateTimeHelper.GetDateTimeOffsetFromXml() method
140-
[2024-08-23] v2.5.0 Create dependency injection registration for IBaseObjectCacheStrategy
141-
[2024-08-23] v2.5.1 Update DateTimeHelper.GetDateTimeOffsetFromXml() method #297 Thanks @zhaoyangguang
142-
[2024-09-11] v2.5.2 Update Cache, remove InsertToCache(), add Count(prefix)
143-
[2024-10-24] v3.0.0-beta1 Upgrade to English version
144-
[2024-10-24] v3.0.0-beta3 Add ApiClient and ApiClientHelper
145-
[2024-11-28] v3.0.1-beta3 Add UseLowerCaseApiName property for SenparcSetting
146-
[2024-12-04] v3.1.0-beta3 update Start() method, set SenparcSetting in Config when AddSenparcGlobalService() run
147-
[2025-01-05] v3.1.1 update Encoding.UTF-8 for SenparcTrace log store
148-
</PackageReleaseNotes>
137+
v2.4.1 Add refresh parameter to SenparcDI.GetServiceProvider() method
138+
v2.4.2 Add ReflectionHelper.HasParameterlessConstructor() method
139+
v2.4.3 Update DateTimeHelper.GetDateTimeOffsetFromXml() method
140+
[2024-08-23] v2.5.0 Create dependency injection registration for IBaseObjectCacheStrategy
141+
[2024-08-23] v2.5.1 Update DateTimeHelper.GetDateTimeOffsetFromXml() method #297 Thanks @zhaoyangguang
142+
[2024-09-11] v2.5.2 Update Cache, remove InsertToCache(), add Count(prefix)
143+
[2024-10-24] v3.0.0-beta1 Upgrade to English version
144+
[2024-10-24] v3.0.0-beta3 Add ApiClient and ApiClientHelper
145+
[2024-11-28] v3.0.1-beta3 Add UseLowerCaseApiName property for SenparcSetting
146+
[2024-12-04] v3.1.0-beta3 update Start() method, set SenparcSetting in Config when AddSenparcGlobalService() run
147+
[2025-01-05] v3.1.1 update Encoding.UTF-8 for SenparcTrace log store
148+
[2025-01-28] v3.1.2 add ValidationHelper.CheckNull
149+
</PackageReleaseNotes>
149150
<RepositoryUrl>https://github.com/Senparc/Senparc.CO2NET</RepositoryUrl>
150151
<Configurations>Debug;Release;Test</Configurations>
151152
</PropertyGroup>

0 commit comments

Comments
 (0)