66
77此函式庫包含多個擴充方法,分為以下類別,適用於多種場景的 C# 開發需求:
88
9- ### ** 1. BinaryExtensions**
9+ ### 1. ` BinaryExtensions `
1010提供針對二進位資料的操作方法:
1111- 寫入分段二進位資料:` Write `
1212- 資料加密與解密(AES):` ToAES ` 、` FromAES `
1313- 資料格式轉換(Base64、Hex、MD5、UTF8):` ToBase64 ` 、` ToHex ` 、` ToMD5 ` 、` ToUTF8 `
1414
15- ### ** 2. CollectionExtensions**
15+ ### 2. ` CollectionExtensions `
1616提供集合類型的操作方法:
1717- 判斷集合是否為空:` IsNullOrEmpty `
1818- 增加或更新字典的元素:` AddRange ` 、` Update ` 、` UpdateRange `
2121- 對字典進行累加或條件移除:` Sum ` 、` RemoveWhere `
2222- 針對 ` HashSet ` 和 ` List ` 提供字串分割並新增元素的功能:` AddRange `
2323
24- ### ** 3. DelegateExtensions**
24+ ### 3. ` DelegateExtensions `
2525提供委託(Delegate)操作的擴充方法:
2626- 合併或移除委託:` Combine ` 、` Remove `
2727- 註冊或取消註冊委託:` Register `
2828- 安全執行委託並獲取結果:` TryInvoke ` 、` GetResults `
2929
30- ### ** 4. MathExtensions**
30+ ### 4. ` MathExtensions `
3131提供數值運算的輔助方法:
3232- 數值的取整與截斷:` Round ` 、` Truncate `
3333- 數值範圍限制與距離計算:` Clamp ` 、` Distance `
3434- 數值轉換(整數、浮點數、布林值、列舉):` ToInt ` 、` ToFloat ` 、` ToBoolean ` 、` ToEnum `
3535- 判斷數值是否為指定列舉類型:` IsDefined `
3636
37- ### ** 5. ReflectionExtensions**
37+ ### 5. ` ReflectionExtensions `
3838提供基於反射的屬性與欄位操作方法:
3939- 取得屬性或欄位值:` GetFieldValue ` 、` GetPropertyValue `
4040- 設定屬性或欄位值:` SetFieldValue ` 、` SetPropertyValue `
4141- 取得類型的指定屬性:` GetAttribute `
4242
43- ### ** 6. StringExtensions**
43+ ### 6. ` StringExtensions `
4444提供針對字串的操作與轉換方法:
4545- 字串比較與串接:` Compare ` 、` Concat `
4646- 格式化與字串分割:` Format ` 、` TrySplit `
4747- 字串轉換(Base64、數值、列舉):` ToBase64 ` 、` ToInt ` 、` ToEnum `
4848- 字串修訂(大小寫、移除子字串):` ToLower ` 、` ToUpper ` 、` Remove `
4949- 判斷字串是否符合條件(空白、字母數字):` IsNullOrEmpty ` 、` IsLetterOrDigit `
5050
51- ### ** 7. TimeExtensions**
51+ ### 7. ` TimeExtensions `
5252提供時間與日期的輔助操作方法:
5353- 計算剩餘時間(分鐘或秒):` GetRemainingMinutes ` 、` GetRemainingSeconds `
5454- 計算時間差(分鐘或秒):` GetDifferenceInMinutes ` 、` GetDifferenceInSeconds `
6363
6464### Binary Extensions
6565
66- 1 . ** Write**
66+ 1 . ` Write `
6767``` csharp
6868public static void Write (this BinaryWriter writer , byte [] data , int buffer )
6969```
@@ -80,7 +80,7 @@ using (var writer = new BinaryWriter(stream))
8080 }
8181```
8282
83- 2 . ** ToAES**
83+ 2 . ` ToAES `
8484``` csharp
8585public static byte [] ToAES (this byte [] data , string key , string iv )
8686```
@@ -92,9 +92,11 @@ string iv = "my-initialization-vector";
9292byte [] encryptedData = originalData .ToAES (key , iv ); // 將資料加密
9393```
9494
95+ ---
96+
9597### Collection Extensions
9698
97- 1 . ** IsNullOrEmpty**
99+ 1 . ` IsNullOrEmpty `
98100``` csharp
99101public static bool IsNullOrEmpty <TSource >(this ICollection < TSource > collection )
100102```
@@ -103,7 +105,7 @@ var collection = new List<int>();
103105bool isEmpty = collection .IsNullOrEmpty (); // 判斷集合是否為 null 或空
104106```
105107
106- 2 . ** TryAdd**
108+ 2 . ` TryAdd `
107109``` csharp
108110public static bool TryAdd <TKey , TValue >(this IDictionary < TKey , TValue > dictionary , TKey key , TValue value )
109111```
@@ -112,7 +114,7 @@ var dictionary = new Dictionary<string, int>();
112114bool added = dictionary .TryAdd (" key1" , 100 ); // 嘗試新增鍵值,若已存在則回傳 false
113115```
114116
115- 3 . ** ForEach**
117+ 3 . ` ForEach `
116118``` csharp
117119public static void ForEach <TSource >(this IEnumerable < TSource > collection , Action < TSource > action )
118120```
@@ -123,9 +125,11 @@ var results = new List<int>();
123125numbers .ForEach (num => results .Add (num * 2 )); // 將每個數字乘以 2 並加入 results
124126```
125127
128+ ---
129+
126130### Delegate Extensions
127131
128- 1 . ** Register**
132+ 1 . ` Register `
129133``` csharp
130134public static TSource Register <TSource >(this TSource source , TSource toRegister , bool isEnable ) where TSource : Delegate
131135```
@@ -137,7 +141,7 @@ action = action.Register(additionalAction, true); // 將 additionalAction 註冊
137141action (5 ); // 執行所有註冊的行為
138142```
139143
140- 2 . ** GetResults**
144+ 2 . ` GetResults `
141145``` csharp
142146public static void GetResults <TResult >(this Func < TResult > func , List < TResult > results )
143147```
@@ -148,9 +152,11 @@ var results = new List<int>();
148152func .GetResults (results ); // 將每次執行的結果加入 results 清單
149153```
150154
155+ ---
156+
151157### Math Extensions
152158
153- 1 . ** Clamp**
159+ 1 . ` Clamp `
154160``` csharp
155161public static float Clamp (this float value , float min , float max )
156162```
@@ -159,7 +165,7 @@ float value = 120f;
159165float clampedValue = value .Clamp (0 f , 100 f ); // 將數值限制在 0 到 100 之間
160166```
161167
162- 2 . ** Distance**
168+ 2 . ` Distance `
163169``` csharp
164170public static float Distance (this float source , float destination )
165171```
@@ -169,9 +175,11 @@ float point2 = 25.3f;
169175float distance = point1 .Distance (point2 ); // 計算兩點之間的距離
170176```
171177
178+ ---
179+
172180### Reflection Extensions
173181
174- 1 . ** GetFieldValue**
182+ 1 . ` GetFieldValue `
175183``` csharp
176184public static object GetFieldValue (this object obj , string name )
177185```
@@ -185,7 +193,7 @@ var sample = new Sample();
185193object fieldValue = sample .GetFieldValue (" _hiddenField" ); // 取得私有欄位的值
186194```
187195
188- 2 . ** SetPropertyValue**
196+ 2 . ` SetPropertyValue `
189197``` csharp
190198public static void SetPropertyValue (this object obj , string name , object value )
191199```
@@ -199,9 +207,11 @@ var sample = new Sample();
199207sample .SetPropertyValue (" Name" , " New Value" ); // 設定屬性值為 "New Value"
200208```
201209
210+ ---
211+
202212### String Extensions
203213
204- 1 . ** GetName**
214+ 1 . ` GetName `
205215``` csharp
206216public static string GetName <TEnum >(this TEnum source ) where TEnum : Enum
207217```
@@ -212,7 +222,7 @@ Colors color = Colors.Green;
212222string name = color .GetName (); // 取得列舉值的名稱 "Green"
213223```
214224
215- 2 . ** ToEnum**
225+ 2 . ` ToEnum `
216226``` csharp
217227public static TEnum ToEnum <TEnum >(this string str ) where TEnum : struct, Enum
218228```
@@ -221,7 +231,7 @@ string colorName = "Blue";
221231Colors color = colorName .ToEnum <Colors >(); // 將字串轉換為列舉值 Colors.Blue
222232```
223233
224- 3 . ** ToLower**
234+ 3 . ` ToLower `
225235``` csharp
226236public static string ToLower (this string source , int index )
227237```
@@ -230,7 +240,7 @@ string input = "HELLO";
230240string result = input .ToLower (0 ); // 將索引 0 的字母轉為小寫,結果為 "hELLO"
231241```
232242
233- 4 . ** Remove**
243+ 4 . ` Remove `
234244``` csharp
235245public static void Remove (this StringBuilder source , string str )
236246```
@@ -239,9 +249,11 @@ var builder = new StringBuilder("Hello, World!");
239249builder .Remove (" World" ); // 移除 "World" 子字串,結果為 "Hello, !"
240250```
241251
252+ ---
253+
242254### Time Extensions
243255
244- 1 . ** GetRemainingSeconds**
256+ 1 . ` GetRemainingSeconds `
245257``` csharp
246258public static double GetRemainingSeconds (this DateTime date )
247259```
@@ -250,7 +262,7 @@ DateTime now = DateTime.Now;
250262double remainingSeconds = now .GetRemainingSeconds (); // 計算當天剩餘秒數
251263```
252264
253- 2 . ** GetDifferenceInSeconds**
265+ 2 . ` GetDifferenceInSeconds `
254266``` csharp
255267public static double GetDifferenceInSeconds (this TimeSpan span , TimeSpan value )
256268```
0 commit comments