Skip to content
Promote Your Product

Extension Methods

Namespace: ZhonTai.Common.Extensions

Provides commonly used extension methods for DateTime, List, Enum, object, and more.

DateTimeExtension

Use Cases

  • Converting between timestamps and DateTime
  • Getting localized weekday names

Examples

csharp
using ZhonTai.Common.Extensions;

// DateTime to timestamp (seconds)
var timestamp = DateTime.Now.ToTimestamp();
// 1700000000

// DateTime to timestamp (milliseconds)
var timestampMs = DateTime.Now.ToTimestamp(milliseconds: true);
// 1700000000000

// Get Chinese weekday name
var week = DateTime.Now.GetWeekName();
// "Monday" (周一)

ListExtension

Use Cases

  • Converting flat lists to tree structures (menus, organization charts)
  • Converting tree structures to flat lists
  • Deep cloning lists

Examples

csharp
using ZhonTai.Common.Extensions;

// Flat list to tree structure
var tree = flatList.ToTree(
    rootWhere: (parent, child) => parent == null && child.ParentId == 0,
    childsWhere: (parent, child) => parent.Id == child.ParentId,
    addChilds: (parent, childs) => parent.Children = childs.ToList()
);

// Tree to flat list
var flatList = tree.ToPlainList(x => x.Children);

// Deep clone list
var cloned = list.Clone();

EnumExtension

Use Cases

  • Getting [Description] attribute values from enums
  • Converting enums to dropdown option lists

Examples

csharp
using ZhonTai.Common.Extensions;

public enum Status
{
    [Description("Enabled")]
    Enabled = 1,
    [Description("Disabled")]
    Disabled = 2
}

// Get enum description
var desc = Status.Enabled.ToDescription();
// "Enabled"

// Get enum name with description
var nameDesc = Status.Enabled.ToNameWithDescription();
// "Enabled(Enabled)"

// Enum to integer
var value = Status.Enabled.ToInt64();
// 1

// Enum to dropdown list
var list = Status.Enabled.ToList();
// [{"Label": "Enabled", "Value": 1}, {"Label": "Disabled", "Value": 2}]

// Generic approach (no instance needed)
var list2 = EnumExtension.ToList<Status>();

UtilConvertExtension

Use Cases

  • Safe type conversion from object
  • Database field value conversion
  • Conversion between byte arrays and hex/Base64

Examples

csharp
using ZhonTai.Common.Extensions;

object value = "123";

// Basic type conversions
var i = value.ToInt();        // 123
var l = value.ToLong();       // 123
var f = value.ToFloat();      // 123.0
var d = value.ToDouble();     // 123.0
var dec = value.ToDecimal();  // 123
var b = value.ToBool();       // false
var date = value.ToDateTime();

// Conversion with default values
var money = "abc".ToMoney(0.0);        // 0.0
var str = null.ToString("default");    // "default"

// Specify decimal places
var d2 = "3.14159".ToDouble(2);  // 3.14

// byte[] conversions
var bytes = new byte[] { 0x12, 0x34 };
var hex = bytes.ToHex();        // "1234" (lowercase by default)
var hex2 = bytes.ToHex(false);  // "1234" (uppercase)
var b64 = bytes.ToBase64();     // "EjQ="

// Hex string to byte[]
var bytes2 = "1234".HexToBytes();

// Timestamp to DateTime
var dt = 1700000000L.ToDateTime();