Skip to content

Commit

Permalink
Merge pull request #8 from TimChen44/main
Browse files Browse the repository at this point in the history
增加clone和collections的扩展函数
  • Loading branch information
TimChen44 authored Sep 8, 2023
2 parents 98a01f9 + bd549d3 commit cca233c
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 2 deletions.
12 changes: 12 additions & 0 deletions EasyTool.Core/CloneCategory/CloneExtension.cs
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
using System.Linq;
using System.Text;

namespace EasyTool
namespace EasyTool.Extension
{
public static class DictionaryUtil
public static class DictionaryExtension
{
/// <summary>
/// 获取字典中指定键的值,如果字典中不存在该键,则返回默认值
Expand Down
1 change: 1 addition & 0 deletions EasyTool.Core/CollectionsCategory/IteratorUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace EasyTool
{
//TODO:疑问,这些功能Linq支持吗?
/// <summary>
/// 迭代器工具类
/// </summary>
Expand Down
30 changes: 30 additions & 0 deletions EasyTool.Core/CollectionsCategory/LinkedListExtension.cs
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);

}
}
19 changes: 19 additions & 0 deletions EasyTool.Core/CollectionsCategory/ListExtension.cs
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);
}
}
33 changes: 33 additions & 0 deletions EasyTool.Core/CollectionsCategory/QueueExtension.cs
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);

}
}
19 changes: 19 additions & 0 deletions EasyTool.Core/CollectionsCategory/StackExtension.cs
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);
}
}
107 changes: 107 additions & 0 deletions EasyTool.Core/DateTimeCategory/DateTimeExtension.cs
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);
}
}
56 changes: 56 additions & 0 deletions EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs
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; }

Check warning on line 42 in EasyTool.CoreTests/CloneCategory/CloneExtensionTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'MyProperty2' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public Second Second1 { get; set; }

public Second Second2 { get; set; }
}

[Serializable]
public class Second
{
public int MyProperty1 { get; set; }
public string MyProperty2 { get; set; }
}
}
}

0 comments on commit cca233c

Please sign in to comment.