Skip to content

Commit 1d6167e

Browse files
committed
💾 ✔️ Feat, Test(Utils): Extends string to be thrown as exception message.
1 parent b4554f9 commit 1d6167e

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

Common.BasicHelper.Test/Utils/Extensions/StringHelper_Tests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,10 @@ public async Task Test_ReadAllTextFromDiskAsync()
7878

7979
Assert.AreEqual(read, "Test");
8080
}
81+
82+
[TestMethod()]
83+
public void Test_Throw()
84+
{
85+
Assert.ThrowsException<ArgumentException>("Exception Message".Throw<ArgumentException>);
86+
}
8187
}

Common.BasicHelper/Utils/Extensions/StringHelper.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,15 @@ void normalSeparate()
105105
/// <param name="str">字符串对象</param>
106106
/// <returns>是否为空或仅有空白组成</returns>
107107
public static bool IsNullOrWhiteSpace(this string? str) => string.IsNullOrWhiteSpace(str);
108+
109+
/// <summary>
110+
/// 将字符串作为异常消息抛出
111+
/// </summary>
112+
/// <typeparam name="T">异常类型</typeparam>
113+
/// <param name="message">异常消息</param>
114+
public static void Throw<T>(this string? message) where T : Exception
115+
{
116+
var exp = Activator.CreateInstance(typeof(T), message);
117+
throw (exp as T) ?? new Exception(message);
118+
}
108119
}

0 commit comments

Comments
 (0)