Hanson.Common.Extensions 2.0.1

dotnet add package Hanson.Common.Extensions --version 2.0.1                
NuGet\Install-Package Hanson.Common.Extensions -Version 2.0.1                
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="Hanson.Common.Extensions" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hanson.Common.Extensions --version 2.0.1                
#r "nuget: Hanson.Common.Extensions, 2.0.1"                
#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.
// Install Hanson.Common.Extensions as a Cake Addin
#addin nuget:?package=Hanson.Common.Extensions&version=2.0.1

// Install Hanson.Common.Extensions as a Cake Tool
#tool nuget:?package=Hanson.Common.Extensions&version=2.0.1                

README

Hanson.Common.Utils

主要功能

  1. 提供 DateTime 相關 Extension

    • 字串轉 DateTime
    • DateTime 轉 Unix Timestamp (long)
    • Unix Timestamp 轉 DateTime
  2. 提供 string 相關 Extension

    • string 轉 bool
    • string 轉 int
    • string 轉 double
    • string 轉 float
    • string 轉 Guid
    • string 生成 SHA256
    • string 生成 MD5
    • string 加密 AES256
    • string 解密 AES256
    • string AES 加密
    • string AES 解密
  3. 提供 Enum 相關 Extension

    • string 轉成 Enum
    • 擴充 EnumNameAttribute
    • 取得 Description 內容
    • 取得 EnumName 內容
  4. 提供 byte 相關 Extension

    • byte array 轉成 16進制文字
  5. 提供 JSON 相關 Extension

    • JSON 字串轉成 class
    • class 轉成 JSON 字串
  6. 提供 XML 相關 Extension

    • XML 字串轉成 class
    • class 轉成 XML 字串
  7. 提供 Regex 相關 Extension

    • 檢查字串是否符合 Regex
    • 取得符合 Regex 的文字內容
  8. 提供 SystemTime 功能

    • 指定 Current Time
    • 重置 Current Time 改取 DateTime.Now

前置條件

  • 開發環境需具備 .Net 8.0
  • 運行於 Windows Platform (x64)
  • 運行於 Linux Platform (x64)
  • 運行於 Raspberry PI Platform (x64)

安裝方式

授權

此專案採用的 License為 Apache-2.0

使用範例

命名空間

using Hanson.Common.Utils;
  • DateTime Extension sample
private void Sample()
{
    // string 轉成 DateTime
    DateTime time = "2024/01/25 08:00:01".ToDateTime();
    Console.WriteLine($"string (2024/01/25 08:00:01) => DateTime {time:yyyy-MM-dd HH:mm:ss}");
    
    // 支援不使用分隔符號的 YYYYMMDDHHmmss 格式
    time = "20240125080001".ToDateTime();
    Console.WriteLine($"string (20240125080001) => DateTime {time:yyyy-MM-dd HH:mm:ss}");

    // DateTime 轉成 Unix timestamp
    long unix = time.ToUnixTimestamp();
    Console.WriteLine($"DateTime ({time}) => Unix timestamp (ms):{unix}");

    // Unix timestamp 轉成 DateTime
    DateTime unixTime = unix.ToDateTime();
    Console.WriteLine($"Unix timestamp ({unix}) => DateTime {time:yyyy-MM-dd HH:mm:ss}");

}
  • String Extension Sample
private void Sample()
{
    string value;

    // string 轉成 bool
    value = "true";
    Console.WriteLine($"string ({value}) => bool({value.ToBool()})");

    // string 轉成 int
    value = "100";
    Console.WriteLine($"string ({value}) => int({value.ToInt()})");

    // string 轉成 float
    value = "100.1";
    Console.WriteLine($"string ({value}) => float({value.ToFloat()})");

    // string 轉成 double
    value = "100.001";
    Console.WriteLine($"string ({value}) => double({value.ToDouble()})");

    // string 轉成 GUID
    value = "b8452395-24dc-4015-86f0-9bc44a691a87";
    Console.WriteLine($"string ({value}) => GUID:{value.ToGuid()}");

    // string 轉成 SHA256
    value = "TEST SHA";
    Console.WriteLine($"string ({value}) => SHA256:{value.ToSHA256()}");

    // string 轉成 MD5
    value = "TEST MD5";
    Console.WriteLine($"string ({value}) => MD5:{value.ToMD5()}");

    // string 加密 AES256
    value = "TEST AES256";
    Console.WriteLine($"string ({value}) => AES256:{value.ToEncryptAES256("12345678901234567890123456789012")}");

    // string 解密 AES256
    value = "AM6TpAVEsZnQ1UXRU7OKujR6Kxjbp5qs+uB7RMvvdpM=";
    Console.WriteLine($"string ({value}) => AES256:{value.ToEncryptAES256("12345678901234567890123456789012")}");

}
  • Byte array Extension Sample
private void Sample()
{
    byte[] values = new byte[] { 0x00, 0xAB, 0xFF };

    // byte array 轉成 hex string
    Console.WriteLine($"bytes  ({{ 0x00, 0xAB, 0xFF }}) => 16進制字串 ({values.ToHexString()})");

    // byte list 轉成 hex string
    Console.WriteLine($"bytes  ({{ 0x00, 0xAB, 0xFF }}) => 16進制字串 ({values.ToList().ToHexString()})");
}
  • Enum Extension Sample
public enum ForkEnum
{
    [EnumName("Name")]
    [Description("Test")]
    Test
}

private void Sample()
{
    ForkEnum value = ForkEnum.Test;
    
	// 取得 EnumName 的內容
	Console.WriteLine($"取得 ForkEnum.Test 設定的 Name ({value.GetName()})");
    
	// 取得 Description 的內容
	Console.WriteLine($"取得 ForkEnum.Test 設定的 Description ({value.GetDescription()})");
}
  • Regex Extension Sample
private void Sample()
{
    string pattern = @"^[\w+]*$";
    string value = "ABCabc123";
    
    // 檢查字串是否符合 regex
    Console.WriteLine($"檢查字串 ({value}) 是否符合 pattern: {value.ValidRegex(pattern)}");

    // 取得符合 regex 的字串資訊
    RegexValue regex = value.GetRegexValue(pattern);
    Console.WriteLine($"取得符合 pattern 的字串: {regex.Value}, 開始位置:{regex.Index}, 長度:{regex.Length}");

}
  • XML Extension Sample
public class ForkClass
{
    public string Name { get; set; }

    public double Age { get; set; }
}

private void Sample()
{
    ForkClass value = new ForkClass
    {
        Name = "Hanson",
        Age = 30
    };

    // 把 Class 轉成 XML 字串
    string xmlStr = value.ToXMLSerialize();
    Console.WriteLine($"把 Class 轉成 XML 字串: {xmlStr}");

    // 把 XML 字串 轉成 Class
    ForkClass xmlClass = xmlStr.ToXMLDeserialize<ForkClass>();
    Console.WriteLine($"把 XML 字串 轉成 Class Name: {xmlClass.Name}, Age:{xmlClass.Age}");

}
  • JSON Extension Sample
public class ForkClass
{
    public string Name { get; set; }

    public double Age { get; set; }
}

private  void Sample()
{
    ForkClass value = new ForkClass
    {
        Name = "Hanson",
        Age = 30
    };

    // 把 Class 轉成 JSON 字串
    string jsonStr = value.ToJsonSerialize(Newtonsoft.Json.Formatting.Indented);
    Console.WriteLine($"把 Class 轉成 JSON 字串: {jsonStr}");

    // 把 JSON 字串 轉成 Class
    ForkClass jsonClass = jsonStr.ToJsonDeserialize<ForkClass>();
    Console.WriteLine($"把 JSON 字串 轉成 Class Name: {jsonClass.Name}, Age:{jsonClass.Age}");

    // 檢查字串是否符合 JSON
    Console.WriteLine($"檢查字串 ({jsonStr}) 是否符合 JSON: {jsonStr.ValidJsonFormat()}");

}
  • SystemTime Sample
private  void Sample()
{
    DateTime currentTime = new DateTime(1981, 1, 1);

    // 使用指定的系統預設時間
    SystemTime.Set(currentTime);
    Console.WriteLine($"系統目前時間: {SystemTime.Now()}");
    
    // 使用重置系統預設時間,改用 DateTime.Now
    SystemTime.Reset();
    Console.WriteLine($"系統目前時間: {SystemTime.Now()}");
}
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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on Hanson.Common.Extensions:

Package Downloads
Hanson.AutoUpdater

Package Description

Hanson.Common.FileUtils

Package Description

Hanson.Cache.SQLite

Package Description

Hanson.Mqtt.Utils

Hanson.Mqtt.Utils 支援項目 1- Broker Server 2- Pub/Sub 功能

Hanson.Common.ScheduleUtils

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 116 11/30/2024
1.1.0 121 11/4/2024