WCP.Extensions 8.0.0

dotnet add package WCP.Extensions --version 8.0.0
                    
NuGet\Install-Package WCP.Extensions -Version 8.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WCP.Extensions" Version="8.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WCP.Extensions" Version="8.0.0" />
                    
Directory.Packages.props
<PackageReference Include="WCP.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WCP.Extensions --version 8.0.0
                    
#r "nuget: WCP.Extensions, 8.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WCP.Extensions@8.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WCP.Extensions&version=8.0.0
                    
Install as a Cake Addin
#tool nuget:?package=WCP.Extensions&version=8.0.0
                    
Install as a Cake Tool

WCP.Extensions

WCP.Extensions 是一组面向日常业务场景的 C# 扩展方法集合,覆盖字符串处理、HTML/SQL 过滤、防注入、LINQ 条件组合、时间与路径工具、枚举 Display 读取等常用功能,帮助你在 .NET 项目中减少样板代码、提升开发效率。

  • Target Framework: net8.0
  • License: Apache-2.0
  • 作者 / Author: 吴存平

安装 (Install)

在 NuGet 中搜索并安装:

  • 包名:WCP.Extensions

使用 .NET CLI:

dotnet add package WCP.Extensions

使用 Package Manager:

Install-Package WCP.Extensions

PackageReference 示例:

<ItemGroup>
  <PackageReference Include="WCP.Extensions" Version="8.0.0" />
</ItemGroup>

版本号以实际发布到 NuGet 为准。


核心功能 (Key Features)

命名空间:WCP.Extensions

主要扩展类:

  • FilterExtension:SQL/HTML 过滤、摘要生成、图片懒加载标签替换等;
  • LinqExtension:基于字符串属性名的排序、WhereIf/WhereIfElse 条件组合、Lambda And/Or 拼接等;
  • UtilitiesExtension:时间戳与 DateTime 互转、字符串/数组工具、敏感信息脱敏(手机、身份证、银行卡、邮箱、姓名)、Display/DisplayName 读取等;
  • 其他如 TypeConverterExtension、ValidatorExtension 等,可从 XML 注释中查看具体 API。

示例:过滤输入与 HTML 文本 (FilterExtension)

using WCP.Extensions;

string keyword = "select * from Users where Id=1;";

// 过滤 SQL 注入关键字 + 标点
string safe = keyword.FilterSql();

string html = "<p>测试<b>内容</b><script>alert(1)</script></p>";

// 去掉脚本和标签,得到纯文本摘要
string summary = html.GetSummary(50);

处理图片懒加载:

string html = "<img src=\"/img/a.jpg\" /><img src=\"/img/b.jpg\" />";

// 将 img 的真实地址放到 data-src,上屏时使用占位图
string lazyHtml = html.ReplaceImg("/images/placeholder.png");

// 将 lazyload 形式还原成普通 img
string restored = lazyHtml.RestoreImg();

示例:LINQ 条件组合与动态排序 (LinqExtension)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using WCP.Extensions;

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

var users = new List<User>
{
    new() { Id = 1, Name = "张三", Age = 18 },
    new() { Id = 2, Name = "李四", Age = 25 },
    new() { Id = 3, Name = "王五", Age = 30 }
}.AsQueryable();

bool filterByAge = true;
int minAge = 20;

Expression<Func<User, bool>> predicate = u => true;

// 按需要拼接条件
predicate = predicate.WhereIf(filterByAge, u => u.Age >= minAge);

// 动态排序(根据属性名)
var result = users.Where(predicate).OrderBy("Name");

示范:时间/路径与字符串工具 (UtilitiesExtension)

时间戳与 DateTime:

using WCP.Extensions;

DateTime now = DateTime.Now;
long ts13 = now.GetTimeStamp();      // 13 位毫秒时间戳
long ts10 = now.GetUnixTimeStamp();  // 10 位秒时间戳

DateTime dtFromTs = ts13.ToDateTime();

string friendly = now.ToDateTimeString(DateTime.Now.AddMinutes(5)); // 刚刚/xx分钟前/今天 HH:mm 等

路径拼接与字符串:

string baseUrl = "https://example.com/";
string full = baseUrl.PathCombine("/api", "v1", "users");
// => https://example.com/api/v1/users

string phone = "13887615970";
string safePhone = phone.ReplacePhone(); // 138****5970

string idCard = "511602198006275098";
string safeIdCard = idCard.ReplaceIdCard(); // 5116**********5098

Display/DisplayName 读取:

using System.ComponentModel.DataAnnotations;
using WCP.Extensions;

public enum OrderStatus
{
    [Display(Name = "待支付")]
    Pending = 0,
    [Display(Name = "已支付")]
    Paid = 1
}

string text = OrderStatus.Paid.GetDisplay(); // "已支付"
int value = OrderStatus.Paid.GetValue();     // 1

注意事项 (Notes)

  • 请在代码中引用正确命名空间:using WCP.Extensions;。
  • 动态排序(OrderBy(string name) 等)依赖反射,适用于通用查询场景,极端高性能场景下请谨慎使用。
  • FilterSql 仅用于简单防护,不能替代参数化查询,请始终使用参数化 SQL 或 ORM。

License

本项目使用 Apache-2.0 许可证。详情请参阅 NuGet 包或源码中的 LICENSE 说明。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.