-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from TimChen44/main
增加clone和collections的扩展函数
- Loading branch information
Showing
9 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace EasyTool.Extension | ||
{ | ||
public static class CloneExtension | ||
{ | ||
//定义一个泛型方法,接受一个泛型参数 T,并返回一个 T 类型的对象 | ||
public static T Clone<T>(this T obj)=> CloneUtil.Clone(obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
namespace EasyTool | ||
{ | ||
//TODO:疑问,这些功能Linq支持吗? | ||
/// <summary> | ||
/// 迭代器工具类 | ||
/// </summary> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace EasyTool.Extension | ||
{ | ||
|
||
/// <summary> | ||
/// 双向链表工具类 | ||
/// </summary> | ||
public static class LinkedListExtension | ||
{ | ||
/// <summary> | ||
/// 将双向链表中的某个节点移动到链表的结尾处。 | ||
/// </summary> | ||
/// <typeparam name="T">双向链表元素类型</typeparam> | ||
/// <param name="list">双向链表</param> | ||
/// <param name="node">要移动的节点</param> | ||
public static void MoveLast<T>(this LinkedList<T> list, LinkedListNode<T> node) => LinkedListUtil.MoveLast(list,node); | ||
|
||
/// <summary> | ||
/// 将双向链表中移动到最前方 | ||
/// </summary> | ||
/// <typeparam name="T">双向链表元素类型</typeparam> | ||
/// <param name="list">双向链表</param> | ||
/// <param name="node">要移动的节点</param> | ||
public static void MoveFirst<T>(this LinkedList<T> list, LinkedListNode<T> node) => LinkedListUtil.MoveFirst(list,node); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace EasyTool.Extension | ||
{ | ||
public static class ListExtension | ||
{ | ||
/// <summary> | ||
/// 判断两个列表是否相等。 | ||
/// </summary> | ||
/// <typeparam name="T">列表元素类型</typeparam> | ||
/// <param name="list1">要比较的第一个列表</param> | ||
/// <param name="list2">要比较的第二个列表</param> | ||
/// <returns>如果两个列表相等,则返回 true;否则返回 false</returns> | ||
public static bool Equals<T>(this List<T> list1, List<T> list2)=> ListUtil.Equals(list1, list2); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace EasyTool.Extension | ||
{ | ||
/// <summary> | ||
/// 队列工具类 | ||
/// </summary> | ||
public static class QueueExtension | ||
{ | ||
|
||
/// <summary> | ||
/// 将指定集合中的元素添加到队列的末尾。 | ||
/// </summary> | ||
/// <typeparam name="T">队列元素类型</typeparam> | ||
/// <param name="queue">队列</param> | ||
/// <param name="collection">要添加到队列中的集合</param> | ||
public static void EnqueueRange<T>(this Queue<T> queue, IEnumerable<T> collection) => QueueUtil.EnqueueRange(queue, collection); | ||
|
||
/// <summary> | ||
/// 从队列中移除指定元素的第一个匹配项。 | ||
/// </summary> | ||
/// <typeparam name="T">队列元素类型</typeparam> | ||
/// <param name="queue">队列</param> | ||
/// <param name="item">要移除的元素</param> | ||
/// <returns>如果已成功移除元素,则为 true;否则为 false。</returns> | ||
public static bool Remove<T>(this Queue<T> queue, T item) => QueueUtil.Remove(queue, item); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace EasyTool.CollectionsCategory | ||
{ | ||
public static class StackExtension | ||
{ | ||
/// <summary> | ||
/// 从堆栈中移除指定元素的第一个匹配项。 | ||
/// </summary> | ||
/// <typeparam name="T">堆栈元素类型</typeparam> | ||
/// <param name="stack">堆栈</param> | ||
/// <param name="item">要移除的元素</param> | ||
/// <returns>如果已成功移除元素,则为 true;否则为 false。</returns> | ||
public static bool Remove<T>(this Stack<T> stack, T item)=> StackUtil.Remove(stack, item); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Text; | ||
|
||
namespace EasyTool.Extension | ||
{ | ||
/// <summary> | ||
/// 提供各种日期操作和计算的工具类。 | ||
/// </summary> | ||
public static class DateTimeExtension | ||
{ | ||
/// <summary> | ||
/// 获取指定日期所在周的第一天的日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在周的第一天的日期。</returns> | ||
public static DateTime GetFirstDayOfWeek(this DateTime date) => DateTimeUtil.GetFirstDayOfWeek(date); | ||
|
||
/// <summary> | ||
/// 获取指定日期所在月份的第一天的日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在月份的第一天的日期。</returns> | ||
public static DateTime GetFirstDayOfMonth(this DateTime date) => DateTimeUtil.GetFirstDayOfMonth(date); | ||
|
||
|
||
/// <summary> | ||
/// 获取指定日期所在季度的第一天的日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在季度的第一天的日期。</returns> | ||
public static DateTime GetFirstDayOfQuarter(this DateTime date) => DateTimeUtil.GetFirstDayOfQuarter(date); | ||
|
||
/// <summary> | ||
/// 获取指定日期所在年份的第一天的日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在年份的第一天的日期。</returns> | ||
public static DateTime GetFirstDayOfYear(this DateTime date) => DateTimeUtil.GetFirstDayOfYear(date); | ||
|
||
/// <summary> | ||
/// 计算指定日期和当前日期之间的天数差。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期和当前日期之间的天数差。</returns> | ||
public static int GetDaysBetween(this DateTime date) => DateTimeUtil.GetDaysBetween(date); | ||
|
||
/// <summary> | ||
/// 计算两个日期之间的天数差。 | ||
/// </summary> | ||
/// <param name="date1">第一个日期。</param> | ||
/// <param name="date2">第二个日期。</param> | ||
/// <returns>两个日期之间的天数差。</returns> | ||
public static int GetDaysBetween(this DateTime date1, DateTime date2) => DateTimeUtil.GetDaysBetween(date1, date2); | ||
|
||
/// <summary> | ||
/// 计算指定日期和当前日期之间的工作日数差。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期和当前日期之间的工作日数差。</returns> | ||
public static int GetWorkDaysBetween(this DateTime date) => DateTimeUtil.GetWorkDaysBetween(date); | ||
|
||
/// <summary> | ||
/// 计算两个日期之间的工作日数差。 | ||
/// </summary> | ||
/// <param name="date1">第一个日期。</param> | ||
/// <param name="date2">第二个日期。</param> | ||
/// <returns>两个日期之间的工作日数差。</returns> | ||
public static int GetWorkDaysBetween(this DateTime date1, DateTime date2) => DateTimeUtil.GetWorkDaysBetween(date1, date2); | ||
|
||
/// <summary> | ||
/// 判断指定日期是否是工作日。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>如果是工作日,则返回 true;否则返回 false。</returns> | ||
public static bool IsWorkDay(this DateTime date) => DateTimeUtil.IsWorkDay(date); | ||
|
||
/// <summary> | ||
/// 获取指定日期所在周的所有日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在周的所有日期。</returns> | ||
public static List<DateTime> GetWeekDays(this DateTime date) => DateTimeUtil.GetWeekDays(date); | ||
|
||
/// <summary> | ||
/// 获取指定日期所在月份的所有日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在月份的所有日期。</returns> | ||
public static List<DateTime> GetMonthDays(this DateTime date) => DateTimeUtil.GetMonthDays(date); | ||
|
||
/// <summary> | ||
/// 获取指定日期所在季度的所有日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在季度的所有日期。</returns> | ||
public static List<DateTime> GetQuarterDays(this DateTime date) => DateTimeUtil.GetQuarterDays(date); | ||
|
||
/// <summary> | ||
/// 获取指定日期所在年份的所有日期。 | ||
/// </summary> | ||
/// <param name="date">指定日期。</param> | ||
/// <returns>指定日期所在年份的所有日期。</returns> | ||
public static List<DateTime> GetYearDays(this DateTime date) => DateTimeUtil.GetYearDays(date); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using EasyTool.Extension; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace EasyTool.Extension.Tests | ||
{ | ||
[TestClass()] | ||
public class CloneExtensionTests | ||
{ | ||
[TestMethod()] | ||
public void CloneTest() | ||
{ | ||
var obj1 = new First() | ||
{ | ||
MyProperty1 = 1, | ||
MyProperty2 = "A", | ||
Second1 = new Second() | ||
{ | ||
MyProperty1 = 2, | ||
MyProperty2 = "B", | ||
}, | ||
Second2 = new Second() | ||
{ | ||
MyProperty1 = 3, | ||
MyProperty2 = "C", | ||
} | ||
}; | ||
var obj2 = obj1.Clone(); | ||
|
||
Assert.AreEqual(obj1.MyProperty1, obj2.MyProperty1); | ||
Assert.AreEqual(obj1.Second1.MyProperty1, obj2.Second1.MyProperty1); | ||
} | ||
|
||
[Serializable] | ||
public class First | ||
{ | ||
public int MyProperty1 { get; set; } | ||
public string MyProperty2 { get; set; } | ||
|
||
public Second Second1 { get; set; } | ||
|
||
public Second Second2 { get; set; } | ||
} | ||
|
||
[Serializable] | ||
public class Second | ||
{ | ||
public int MyProperty1 { get; set; } | ||
public string MyProperty2 { get; set; } | ||
} | ||
} | ||
} |