P.ExtensionMethods
26.2.28.1841
dotnet add package P.ExtensionMethods --version 26.2.28.1841
NuGet\Install-Package P.ExtensionMethods -Version 26.2.28.1841
<PackageReference Include="P.ExtensionMethods" Version="26.2.28.1841" />
<PackageVersion Include="P.ExtensionMethods" Version="26.2.28.1841" />
<PackageReference Include="P.ExtensionMethods" />
paket add P.ExtensionMethods --version 26.2.28.1841
#r "nuget: P.ExtensionMethods, 26.2.28.1841"
#:package P.ExtensionMethods@26.2.28.1841
#addin nuget:?package=P.ExtensionMethods&version=26.2.28.1841
#tool nuget:?package=P.ExtensionMethods&version=26.2.28.1841
P.ExtensionMethods
C#扩展方法集合,提供了一系列实用的扩展方法,帮助简化代码编写。
兼容性说明:
- .NET 6及以上版本:部分Linq扩展(如MinBy/MaxBy/DistinctBy等)已内置于系统,建议优先使用系统自带方法。本库提供的
MinByKey/MaxByKey等方法可用于低版本或自定义场景。- .NET Framework/.NET Standard等低版本:可直接使用本库所有扩展方法。
功能特性
System.Linq 扩展方法
MinByKey/MaxByKey 系列
MinByKey<TSource, TKey>: 返回序列中具有最小键值的元素MaxByKey<TSource, TKey>: 返回序列中具有最大键值的元素MinByKeyOrDefault<TSource, TKey>: 返回序列中具有最小键值的元素,如果序列为空则返回默认值MaxByKeyOrDefault<TSource, TKey>: 返回序列中具有最大键值的元素,如果序列为空则返回默认值
使用示例:
var people = new List<Person>
{
new Person { Name = "Alice", Age = 30 },
new Person { Name = "Bob", Age = 25 },
new Person { Name = "Charlie", Age = 35 }
};
// 获取年龄最小的人
var youngest = people.MinByKey(p => p.Age);
// 结果: Bob, 25岁
// 获取年龄最大的人
var oldest = people.MaxByKey(p => p.Age);
// 结果: Charlie, 35岁
// 空序列时返回默认值
var emptyList = new List<Person>();
var result = emptyList.MinByKeyOrDefault(p => p.Age);
// 结果: null
System.Collections.Generic 扩展方法
IsNullOrEmpty / HasItems
IsNullOrEmpty<T>: 判断集合是否为null或空HasItems<T>: 判断集合是否包含元素
使用示例:
List<string> emptyList = new List<string>();
bool isEmpty = emptyList.IsNullOrEmpty(); // 返回true
bool hasItems = emptyList.HasItems(); // 返回false
List<string> nameList = new List<string> { "张三", "李四" };
bool nameListEmpty = nameList.IsNullOrEmpty(); // 返回false
bool nameListHasItems = nameList.HasItems(); // 返回true
System.DateTime 扩展方法
节假日判断系列方法
提供了丰富的中国节假日判断方法,支持自动识别法定节假日、调休工作日等。数据来源包括本地缓存、在线API等多种途径,确保数据的准确性和可用性。
IsHoliday / IsWorkingDay
判断日期是否为节假日或工作日。
DateTime newYear = new DateTime(2023, 1, 1); // 元旦
bool isHoliday = newYear.IsHoliday(); // 返回true
bool isWorkingDay = newYear.IsWorkingDay(); // 返回false
// 五一劳动节的调休工作日(周六补班)
DateTime workingSaturday = new DateTime(2023, 4, 23);
bool isWorkingWeekend = workingSaturday.IsWorkingDay(); // 可能返回true,取决于当年节假日安排
GetHolidayName
获取节假日名称。
DateTime springFestival = new DateTime(2023, 1, 22);
string holidayName = springFestival.GetHolidayName(); // 返回"春节"
DateTime normalDay = new DateTime(2023, 3, 1);
string name = normalDay.GetHolidayName(); // 返回空字符串
GetDayIndexInContinuousRange
获取指定日期在连续相同类型日期中的第几天(从1开始计数)。
// 假设5月1日至5月5日是连续节假日
DateTime holiday = new DateTime(2023, 5, 3);
int dayIndex = holiday.GetDayIndexInContinuousRange(); // 返回3,表示连续假期的第3天
// 假设4月17日至4月21日是连续工作日
DateTime workday = new DateTime(2023, 4, 19);
int workdayIndex = workday.GetDayIndexInContinuousRange(); // 返回3,表示连续工作日的第3天
GetNthWorkingDayOfMonth / GetNthHolidayOfMonth
获取当月第N个工作日或节假日。
DateTime date = new DateTime(2023, 10, 15);
// 获取10月份第一个工作日
DateTime firstWorkingDay = date.GetNthWorkingDayOfMonth(1);
// 获取10月份最后一个工作日
DateTime lastWorkingDay = date.GetNthWorkingDayOfMonth(-1);
// 获取10月份第一个节假日
DateTime firstHoliday = date.GetNthHolidayOfMonth(1);
// 获取10月份最后一个节假日
DateTime lastHoliday = date.GetNthHolidayOfMonth(-1);
IsFirstHalfOfWorkingWeek / IsSecondHalfOfWorkingWeek
判断日期是否为工作日区间的前半区或后半区。工作日区间指的是连续工作日的范围。
// 假设2025年4月27日到4月30日是连续工作日区间
DateTime firstDay = new DateTime(2025, 4, 27);
bool isFirstHalf = firstDay.IsFirstHalfOfWorkingWeek(); // 返回true,属于区间前半部分
DateTime lastDay = new DateTime(2025, 4, 30);
bool isSecondHalf = lastDay.IsSecondHalfOfWorkingWeek(); // 返回true,属于区间后半部分
IsFirstHalfOfHolidayWeek / IsSecondHalfOfHolidayWeek
判断日期是否为假日区间的前半区或后半区。假日区间指的是连续假日的范围。
// 假设2025年5月1日到5月5日是连续假日区间
DateTime firstDay = new DateTime(2025, 5, 1);
bool isFirstHalf = firstDay.IsFirstHalfOfHolidayWeek(); // 返回true,属于区间前半部分
DateTime lastDay = new DateTime(2025, 5, 5);
bool isSecondHalf = lastDay.IsSecondHalfOfHolidayWeek(); // 返回true,属于区间后半部分
IsFirstHalfOfWeek / IsSecondHalfOfWeek
判断日期是否为星期的上半周(周一到周三)或下半周(周四到周日)。
DateTime monday = new DateTime(2023, 10, 16); // 周一
bool isFirstHalf = monday.IsFirstHalfOfWeek(); // 返回true
DateTime thursday = new DateTime(2023, 10, 19); // 周四
bool isSecondHalf = thursday.IsSecondHalfOfWeek(); // 返回true
RefreshHolidayData
刷新节假日数据(强制从远程获取最新数据)。
DateTime now = DateTime.Now;
bool success = now.RefreshHolidayData(); // 刷新当年节假日数据
交易日相关方法
IsTradingDay
判断日期是否为证券交易日。
DateTime date = DateTime.Now;
bool isTradingDay = date.IsTradingDay(); // 如果是交易日返回true,否则返回false
IsTradingHours
判断当前时间是否在证券交易时段内。
DateTime now = DateTime.Now;
bool isTrading = now.IsTradingHours(); // 判断是否在9:30-11:30或13:00-15:00的交易时段
IsAmTradingSession / IsPmTradingSession
判断当前时间是否在上午或下午的交易时段内。
DateTime morning = new DateTime(2023, 10, 9, 10, 30, 0); // 上午10:30
bool isAmSession = morning.IsAmTradingSession(); // 返回true
DateTime afternoon = new DateTime(2023, 10, 9, 14, 30, 0); // 下午14:30
bool isPmSession = afternoon.IsPmTradingSession(); // 返回true
GetNextTradingDay / GetPreviousTradingDay
获取下一个或前一个交易日。
DateTime friday = new DateTime(2023, 10, 13); // 周五
DateTime nextTradingDay = friday.GetNextTradingDay(); // 返回下一个交易日(可能是下周一)
DateTime tuesday = new DateTime(2023, 10, 10); // 周二
DateTime prevTradingDay = tuesday.GetPreviousTradingDay(); // 返回前一个交易日(可能是周一)
// 获取N个交易日后的日期
DateTime futureDay = friday.GetNextTradingDay(5); // 返回5个交易日后的日期
CountTradingDays
计算两个日期之间的交易日数量。
DateTime startDate = new DateTime(2023, 10, 1);
DateTime endDate = new DateTime(2023, 10, 31);
int tradingDays = startDate.CountTradingDays(endDate); // 返回这个月内的交易日天数
时间戳转换方法
ToUnixTimestamp / ToUnixTimestampMilliseconds
将DateTime转换为Unix时间戳(秒/毫秒)。
DateTime now = DateTime.Now;
long timestamp = now.ToUnixTimestamp(); // 返回Unix时间戳(秒)
long timestampMs = now.ToUnixTimestampMilliseconds(); // 返回Unix时间戳(毫秒)
IsWeekend / IsWeekday
判断日期是否为周末或工作日。
DateTime saturday = new DateTime(2023, 6, 10); // 周六
bool isWeekend = saturday.IsWeekend(); // 返回true
bool isWeekday = saturday.IsWeekday(); // 返回false
DateTime monday = new DateTime(2023, 6, 12); // 周一
bool mondayIsWeekend = monday.IsWeekend(); // 返回false
bool mondayIsWeekday = monday.IsWeekday(); // 返回true
System.Int32 扩展方法
ToRoman
将整数转换为罗马数字表示。
int number = 2023;
string roman = number.ToRoman(); // 返回"MMXXIII"
int smallNumber = 9;
string smallRoman = smallNumber.ToRoman(); // 返回"IX"
IsEven / IsOdd
判断整数是否为偶数或奇数。
int even = 10;
bool isEven = even.IsEven(); // 返回true
bool isOdd = even.IsOdd(); // 返回false
int odd = 7;
bool oddIsEven = odd.IsEven(); // 返回false
bool oddIsOdd = odd.IsOdd(); // 返回true
System.Collections.Generic.IEnumerable 扩展方法
ForEach
对集合中的每个元素执行指定操作。
List<string> names = new List<string> { "张三", "李四", "王五" };
// 基本版本
names.ForEach(name => Console.WriteLine(name));
// 带索引版本
names.ForEach((name, index) => Console.WriteLine($"{index}: {name}"));
System.Guid 扩展方法
// ... 现有的Guid扩展方法说明 ...
安装
通过NuGet包管理器安装:
dotnet add package P.ExtensionMethods
使用要求
- .NET Standard 2.0
- .NET Framework 4.7.2
许可证
MIT License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 is compatible. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.2
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
- System.Net.Http (>= 4.3.4)
- System.Security.Cryptography.Algorithms (>= 4.3.1)
- System.Text.Json (>= 10.0.3)
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
- System.Net.Http (>= 4.3.4)
- System.Security.Cryptography.Algorithms (>= 4.3.1)
- System.Text.Json (>= 10.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 26.2.28.1841 | 143 | 2/28/2026 |
| 26.2.28.1233 | 124 | 2/28/2026 |
| 25.6.13.1315 | 382 | 6/13/2025 |
| 25.6.12.1839 | 391 | 6/12/2025 |
| 25.6.12.1835 | 853 | 6/12/2025 |
| 22.7.29.2238 | 567 | 7/29/2022 |
| 22.7.29.1036 | 553 | 7/29/2022 |
| 22.7.29.952 | 552 | 7/29/2022 |
| 22.7.29.9 | 529 | 7/29/2022 |
| 22.7.28.22 | 544 | 7/28/2022 |
| 22.7.28.21 | 555 | 7/28/2022 |
| 22.7.28.10 | 550 | 7/28/2022 |
| 22.7.28.9 | 553 | 7/28/2022 |
添加日期扩展方法