|
| 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 | +} |
0 commit comments