Skip to content

Commit 936eed0

Browse files
committed
Update README.md
1 parent 9868998 commit 936eed0

File tree

1 file changed

+268
-2
lines changed

1 file changed

+268
-2
lines changed

README.md

Lines changed: 268 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,268 @@
1-
# CSharpExtensionsLibrary
2-
A comprehensive library of C# extension methods for simplifying and enhancing .NET development.
1+
# C# 擴充方法函式庫
2+
3+
一個專為 C# 開發者設計的擴充方法函式庫,內含多種方便的擴充方法,封裝為 DLL 類別庫,適用於各類 .NET 專案,讓您的程式碼更簡潔且易於維護。
4+
5+
## 功能特色
6+
7+
此函式庫包含多個擴充方法,分為以下類別,適用於多種場景的 C# 開發需求:
8+
9+
### **1. BinaryExtensions**
10+
提供針對二進位資料的操作方法:
11+
- 寫入分段二進位資料:`Write`
12+
- 資料加密與解密(AES):`ToAES``FromAES`
13+
- 資料格式轉換(Base64、Hex、MD5、UTF8):`ToBase64``ToHex``ToMD5``ToUTF8`
14+
15+
### **2. CollectionExtensions**
16+
提供集合類型的操作方法:
17+
- 判斷集合是否為空:`IsNullOrEmpty`
18+
- 增加或更新字典的元素:`AddRange``Update``UpdateRange`
19+
- 遍歷集合並執行操作:`ForEach`
20+
- 提供集合元素的操作(取第一或最後元素、移除、嘗試取得值):`GetFirst``GetLast``RemoveFirst``RemoveLast``TryGetValue`
21+
- 對字典進行累加或條件移除:`Sum``RemoveWhere`
22+
- 針對 `HashSet``List` 提供字串分割並新增元素的功能:`AddRange`
23+
24+
### **3. DelegateExtensions**
25+
提供委託(Delegate)操作的擴充方法:
26+
- 合併或移除委託:`Combine``Remove`
27+
- 註冊或取消註冊委託:`Register`
28+
- 安全執行委託並獲取結果:`TryInvoke``GetResults`
29+
30+
### **4. MathExtensions**
31+
提供數值運算的輔助方法:
32+
- 數值的取整與截斷:`Round``Truncate`
33+
- 數值範圍限制與距離計算:`Clamp``Distance`
34+
- 數值轉換(整數、浮點數、布林值、列舉):`ToInt``ToFloat``ToBoolean``ToEnum`
35+
- 判斷數值是否為指定列舉類型:`IsDefined`
36+
37+
### **5. ReflectionExtensions**
38+
提供基於反射的屬性與欄位操作方法:
39+
- 取得屬性或欄位值:`GetFieldValue``GetPropertyValue`
40+
- 設定屬性或欄位值:`SetFieldValue``SetPropertyValue`
41+
- 取得類型的指定屬性:`GetAttribute`
42+
43+
### **6. StringExtensions**
44+
提供針對字串的操作與轉換方法:
45+
- 字串比較與串接:`Compare``Concat`
46+
- 格式化與字串分割:`Format``TrySplit`
47+
- 字串轉換(Base64、數值、列舉):`ToBase64``ToInt``ToEnum`
48+
- 字串修訂(大小寫、移除子字串):`ToLower``ToUpper``Remove`
49+
- 判斷字串是否符合條件(空白、字母數字):`IsNullOrEmpty``IsLetterOrDigit`
50+
51+
### **7. TimeExtensions**
52+
提供時間與日期的輔助操作方法:
53+
- 計算剩餘時間(分鐘或秒):`GetRemainingMinutes``GetRemainingSeconds`
54+
- 計算時間差(分鐘或秒):`GetDifferenceInMinutes``GetDifferenceInSeconds`
55+
- 取得星期的文字表示:`GetWeekString`
56+
- 轉換時間為 Unix 時間戳記:`ToUnixTime`
57+
58+
---
59+
60+
每個功能分類中的函式都設計簡單易用,能有效提升開發效率,適用於各類 .NET 應用程式。更多使用細節請參考以下的範例或完整文件。
61+
62+
## 使用範例
63+
64+
### Binary Extensions
65+
66+
1. **Write**
67+
```csharp
68+
public static void Write(this BinaryWriter writer, byte[] data, int buffer)
69+
```
70+
```csharp
71+
using System.IO;
72+
73+
var data = new byte[1024];
74+
var buffer = 256;
75+
76+
using (var stream = new FileStream("output.bin", FileMode.Create))
77+
using (var writer = new BinaryWriter(stream))
78+
{
79+
writer.Write(data, buffer); // 將資料分批寫入檔案,每次 256 bytes
80+
}
81+
```
82+
83+
2. **ToAES**
84+
```csharp
85+
public static byte[] ToAES(this byte[] data, string key, string iv)
86+
```
87+
```csharp
88+
var originalData = new byte[] { 1, 2, 3, 4, 5 };
89+
string key = "my-secret-key";
90+
string iv = "my-initialization-vector";
91+
92+
byte[] encryptedData = originalData.ToAES(key, iv); // 將資料加密
93+
```
94+
95+
### Collection Extensions
96+
97+
1. **IsNullOrEmpty**
98+
```csharp
99+
public static bool IsNullOrEmpty<TSource>(this ICollection<TSource> collection)
100+
```
101+
```csharp
102+
var collection = new List<int>();
103+
bool isEmpty = collection.IsNullOrEmpty(); // 判斷集合是否為 null 或空
104+
```
105+
106+
2. **TryAdd**
107+
```csharp
108+
public static bool TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
109+
```
110+
```csharp
111+
var dictionary = new Dictionary<string, int>();
112+
bool added = dictionary.TryAdd("key1", 100); // 嘗試新增鍵值,若已存在則回傳 false
113+
```
114+
115+
3. **ForEach**
116+
```csharp
117+
public static void ForEach<TSource>(this IEnumerable<TSource> collection, Action<TSource> action)
118+
```
119+
```csharp
120+
var numbers = new[] { 1, 2, 3, 4, 5 };
121+
var results = new List<int>();
122+
123+
numbers.ForEach(num => results.Add(num * 2)); // 將每個數字乘以 2 並加入 results
124+
```
125+
126+
### Delegate Extensions
127+
128+
1. **Register**
129+
```csharp
130+
public static TSource Register<TSource>(this TSource source, TSource toRegister, bool isEnable)
131+
```
132+
```csharp
133+
Action<int> action = x => results.Add(x * 2);
134+
Action<int> additionalAction = x => results.Add(x * 3);
135+
136+
action = action.Register(additionalAction, true); // 將 additionalAction 註冊到 action
137+
action(5); // 執行所有註冊的行為
138+
```
139+
140+
2. **GetResults**
141+
```csharp
142+
public static void GetResults<TResult>(this Func<TResult> func, List<TResult> results)
143+
```
144+
```csharp
145+
Func<int> func = () => DateTime.Now.Second;
146+
var results = new List<int>();
147+
148+
func.GetResults(results); // 將每次執行的結果加入 results 清單
149+
```
150+
151+
### Math Extensions
152+
153+
1. **Clamp**
154+
```csharp
155+
public static float Clamp(this float value, float min, float max)
156+
```
157+
```csharp
158+
float value = 120f;
159+
float clampedValue = value.Clamp(0f, 100f); // 將數值限制在 0 到 100 之間
160+
```
161+
162+
2. **Distance**
163+
```csharp
164+
public static float Distance(this float source, float destination)
165+
```
166+
```csharp
167+
float point1 = 10.5f;
168+
float point2 = 25.3f;
169+
float distance = point1.Distance(point2); // 計算兩點之間的距離
170+
```
171+
172+
### Reflection Extensions
173+
174+
1. **GetFieldValue**
175+
```csharp
176+
public static object GetFieldValue(this object obj, string name)
177+
```
178+
```csharp
179+
class Sample
180+
{
181+
private int _hiddenField = 42;
182+
}
183+
184+
var sample = new Sample();
185+
object fieldValue = sample.GetFieldValue("_hiddenField"); // 取得私有欄位的值
186+
```
187+
188+
2. **SetPropertyValue**
189+
```csharp
190+
public static void SetPropertyValue(this object obj, string name, object value)
191+
```
192+
```csharp
193+
class Sample
194+
{
195+
public string Name { get; set; }
196+
}
197+
198+
var sample = new Sample();
199+
sample.SetPropertyValue("Name", "New Value"); // 設定屬性值為 "New Value"
200+
```
201+
202+
### String Extensions
203+
204+
1. **GetName**
205+
```csharp
206+
public static string GetName<TEnum>(this TEnum source)
207+
```
208+
```csharp
209+
enum Colors { Red, Green, Blue }
210+
Colors color = Colors.Green;
211+
212+
string name = color.GetName(); // 取得列舉值的名稱 "Green"
213+
```
214+
215+
2. **ToEnum**
216+
```csharp
217+
public static TEnum ToEnum<TEnum>(this string str) where TEnum : struct, Enum
218+
```
219+
```csharp
220+
string colorName = "Blue";
221+
Colors color = colorName.ToEnum<Colors>(); // 將字串轉換為列舉值 Colors.Blue
222+
```
223+
224+
3. **ToLower**
225+
```csharp
226+
public static string ToLower(this string source, int index)
227+
```
228+
```csharp
229+
string input = "HELLO";
230+
string result = input.ToLower(0); // 將索引 0 的字母轉為小寫,結果為 "hELLO"
231+
```
232+
233+
4. **Remove**
234+
```csharp
235+
public static void Remove(this StringBuilder source, string str)
236+
```
237+
```csharp
238+
var builder = new StringBuilder("Hello, World!");
239+
builder.Remove("World"); // 移除 "World" 子字串,結果為 "Hello, !"
240+
```
241+
242+
### Time Extensions
243+
244+
1. **GetRemainingSeconds**
245+
```csharp
246+
public static double GetRemainingSeconds(this DateTime date)
247+
```
248+
```csharp
249+
DateTime now = DateTime.Now;
250+
double remainingSeconds = now.GetRemainingSeconds(); // 計算當天剩餘秒數
251+
```
252+
253+
2. **GetDifferenceInSeconds**
254+
```csharp
255+
public static double GetDifferenceInSeconds(this TimeSpan span, TimeSpan value)
256+
```
257+
```csharp
258+
TimeSpan start = TimeSpan.FromHours(3);
259+
TimeSpan end = TimeSpan.FromHours(5);
260+
261+
double difference = end.GetDifferenceInSeconds(start); // 計算兩時間差的秒數
262+
```
263+
264+
## 文件說明
265+
每個擴充方法都包含詳細的 XML 註解,提供方法的用途、參數及回傳值說明。您可透過 Visual Studio 的 IntelliSense 或其他 IDE 查看詳細資訊,提升開發效率。
266+
267+
## 授權
268+
此專案基於 GPLv3 授權條款,詳情請參閱 LICENSE 文件。

0 commit comments

Comments
 (0)