整理代码
This commit is contained in:
138
ShrlAlgoToolkit.Core/Extensions/DataTableExtensions.cs
Normal file
138
ShrlAlgoToolkit.Core/Extensions/DataTableExtensions.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System.Collections;
|
||||
using System.Data;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Core.Extensions;
|
||||
|
||||
public static class DataTableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取数据
|
||||
/// </summary>
|
||||
/// <param name="dt"></param>
|
||||
/// <param name="row"></param>
|
||||
/// <param name="column"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetValue(this DataTable dt, int row, int column)
|
||||
{
|
||||
return dt != null ? dt.Rows[row][column].ToString() : string.Empty;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取值
|
||||
/// </summary>
|
||||
/// <param name="dr"></param>
|
||||
/// <param name="column"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static string GetValue(this DataRow dr, int column)
|
||||
{
|
||||
return dr == null ? throw new ArgumentNullException(nameof(dr)) : dr[column].ToString();
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置值
|
||||
/// </summary>
|
||||
/// <param name="dt"></param>
|
||||
/// <param name="row"></param>
|
||||
/// <param name="column"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void SetValue(this DataTable dt, int row, int column, object value)
|
||||
{
|
||||
dt.Rows[row][column] = value;
|
||||
}
|
||||
/// <summary>
|
||||
/// 设置值
|
||||
/// </summary>
|
||||
/// <param name="dr"></param>
|
||||
/// <param name="column"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static void SetValue(this DataRow dr, int column, object value)
|
||||
{
|
||||
if (dr == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(dr));
|
||||
}
|
||||
|
||||
dr[column] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转实体,属性名必须与列名相同
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="table"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> ToEntities<T>(this DataTable table)
|
||||
where T : new()
|
||||
{
|
||||
var entities = new List<T>();
|
||||
if (table == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
foreach (DataRow row in table.Rows)
|
||||
{
|
||||
T entity = new();
|
||||
foreach (var item in entity.GetType().GetProperties())
|
||||
{
|
||||
if (table.Columns.Contains(item.Name))
|
||||
{
|
||||
if (DBNull.Value != row[item.Name])
|
||||
{
|
||||
var newType = item.PropertyType;
|
||||
//判断type类型是否为泛型,因为nullable是泛型类,
|
||||
if (newType.IsGenericType && newType.GetGenericTypeDefinition() == typeof(Nullable<>)) //判断convertsionType是否为nullable泛型类
|
||||
{
|
||||
//如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换
|
||||
var nullableConverter = new System.ComponentModel.NullableConverter(newType);
|
||||
//将type转换为nullable对的基础基元类型
|
||||
newType = nullableConverter.UnderlyingType;
|
||||
}
|
||||
|
||||
item.SetValue(entity, Convert.ChangeType(row[item.Name], newType), null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
entities.Add(entity);
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将指定的集合转换成DataTable。
|
||||
/// </summary>
|
||||
/// <param name="list">将指定的集合。</param>
|
||||
/// <returns>返回转换后的DataTable。</returns>
|
||||
public static DataTable ToDataTable(this IList list)
|
||||
{
|
||||
var table = new DataTable();
|
||||
|
||||
var properties = list[0].GetType().GetProperties();
|
||||
foreach (var pi in properties)
|
||||
{
|
||||
var pt = pi.PropertyType;
|
||||
if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
pt = pt.GetGenericArguments()[0];
|
||||
}
|
||||
|
||||
table.Columns.Add(new DataColumn(pi.Name, pt));
|
||||
}
|
||||
|
||||
for (var i = 0; i < list.Count; i++)
|
||||
{
|
||||
var tempList = new ArrayList();
|
||||
foreach (var pi in properties)
|
||||
{
|
||||
var obj = pi.GetValue(list[i], null);
|
||||
tempList.Add(obj);
|
||||
}
|
||||
|
||||
var array = tempList.ToArray();
|
||||
table.LoadDataRow(array, true);
|
||||
}
|
||||
return table;
|
||||
}
|
||||
}
|
||||
49
ShrlAlgoToolkit.Core/Extensions/DistinctExtensions.cs
Normal file
49
ShrlAlgoToolkit.Core/Extensions/DistinctExtensions.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
namespace ShrlAlgo.Toolkit.Core.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// 自定义Distinct扩展方法
|
||||
/// </summary>
|
||||
public static class DistinctExtensions
|
||||
{
|
||||
|
||||
public static IEnumerable<T> Distinct<T>(
|
||||
this IEnumerable<T> source, Func<T, T, bool> comparer)
|
||||
where T : class
|
||||
=> source.Distinct(new DynamicEqualityComparer<T>(comparer));
|
||||
public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
|
||||
=> source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
|
||||
private sealed class DynamicEqualityComparer<T> : IEqualityComparer<T>
|
||||
where T : class
|
||||
{
|
||||
private readonly Func<T, T, bool> _func;
|
||||
|
||||
public DynamicEqualityComparer(Func<T, T, bool> func)
|
||||
{
|
||||
_func = func;
|
||||
}
|
||||
|
||||
public bool Equals(T x, T y) => _func(x, y);
|
||||
|
||||
public int GetHashCode(T obj) => 0;
|
||||
}
|
||||
|
||||
private sealed class CommonEqualityComparer<T, V> : IEqualityComparer<T>
|
||||
{
|
||||
private Func<T, V> keySelector;
|
||||
|
||||
public CommonEqualityComparer(Func<T, V> keySelector)
|
||||
{
|
||||
this.keySelector = keySelector;
|
||||
}
|
||||
|
||||
public bool Equals(T x, T y)
|
||||
{
|
||||
return EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
|
||||
}
|
||||
|
||||
public int GetHashCode(T obj)
|
||||
{
|
||||
return EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
|
||||
}
|
||||
}
|
||||
}
|
||||
102
ShrlAlgoToolkit.Core/Extensions/ImageExtensions.cs
Normal file
102
ShrlAlgoToolkit.Core/Extensions/ImageExtensions.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using ShrlAlgo.Toolkit.Core.Assist;
|
||||
|
||||
using System.Drawing;
|
||||
using System.Drawing.Imaging;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace ShrlAlgo.Toolkit.Core.Extensions;
|
||||
|
||||
public static class ImageExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 提取资源
|
||||
/// </summary>
|
||||
/// <param name="resourceName"></param>
|
||||
/// <param name="path"></param>
|
||||
public static void ExtractResource(string resourceName, string path)
|
||||
{
|
||||
using var manifestResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
|
||||
using var stream = File.Create(path);
|
||||
var array = new byte[8192];
|
||||
int count;
|
||||
while ((count = manifestResourceStream.Read(array, 0, array.Length)) > 0)
|
||||
{
|
||||
stream.Write(array, 0, count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源管理器缩略图读取
|
||||
/// </summary>
|
||||
/// <param name="filename"></param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
public static BitmapSource LoadFileImage(string filename, int width, int height)
|
||||
{
|
||||
var bm = WindowsThumbnailProvider.GetThumbnail(filename, width, height, ThumbnailOptions.None);
|
||||
return Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
}
|
||||
/// <summary>
|
||||
/// 位图转图片
|
||||
/// </summary>
|
||||
/// <param name="bitmap"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static BitmapImage ToBitmapImage(this Bitmap bitmap)
|
||||
{
|
||||
if (bitmap is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bitmap));
|
||||
}
|
||||
|
||||
using var ms = new MemoryStream();
|
||||
bitmap.Save(ms, ImageFormat.Png);
|
||||
|
||||
ms.Position = 0;
|
||||
|
||||
var result = new BitmapImage();
|
||||
|
||||
result.BeginInit();
|
||||
|
||||
result.StreamSource = ms;
|
||||
|
||||
result.CacheOption = BitmapCacheOption.OnLoad;
|
||||
|
||||
result.EndInit();
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 位图转像素集
|
||||
/// </summary>
|
||||
/// <param name="bitmap"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentNullException"></exception>
|
||||
public static BitmapSource ToBitmapSource(this Bitmap bitmap)
|
||||
{
|
||||
if (bitmap == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(bitmap));
|
||||
}
|
||||
return Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Icon转BitmapSource
|
||||
/// </summary>
|
||||
/// <param name="icon"></param>
|
||||
/// <returns></returns>
|
||||
public static BitmapSource ToBitmapSource(this Icon icon)
|
||||
{
|
||||
return Imaging.CreateBitmapSourceFromHIcon(
|
||||
icon.Handle,
|
||||
new Int32Rect(0, 0, icon.Width, icon.Height),
|
||||
BitmapSizeOptions.FromWidthAndHeight(icon.Width, icon.Height)
|
||||
);
|
||||
}
|
||||
}
|
||||
21
ShrlAlgoToolkit.Core/ShrlAlgoToolkit.Core.csproj
Normal file
21
ShrlAlgoToolkit.Core/ShrlAlgoToolkit.Core.csproj
Normal file
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net472</TargetFramework>
|
||||
<OutputType>Library</OutputType>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
<Configurations>Debug;Release</Configurations>
|
||||
<LangVersion>13.0</LangVersion>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWPF>True</UseWPF>
|
||||
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
|
||||
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
|
||||
<RootNamespace>ShrlAlgo.Toolkit.Core</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
|
||||
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Configuration" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user