Table of Contents

Class Toolkit

Namespace
SearchAThing.Ext
Assembly
netcore-ext.dll
public static class Toolkit
Inheritance
Toolkit
Inherited Members

Fields

UnixTimeAutoAmbiguityFrom

public static readonly DateTimeOffset UnixTimeAutoAmbiguityFrom

Field Value

DateTimeOffset

UnixTimeAutoAmbiguityTo

public static readonly DateTimeOffset UnixTimeAutoAmbiguityTo

Field Value

DateTimeOffset

Methods

AppDataFolder(string)

{AppData}/{namespace}/{assembly_name}

public static string AppDataFolder(string ns)

Parameters

ns string

Returns

string

Assign<T>(T, T, Func<PropertyInfo, bool>?)

assign public properties of src to dst object

public static void Assign<T>(T src, T dst, Func<PropertyInfo, bool>? exclude = null)

Parameters

src T
dst T
exclude Func<PropertyInfo, bool>

Type Parameters

T

CopyFromCustom<T>(T, T, CopyFromCustomDelegate?)

copy properties from other object ; a custom non null delegate function can be passed to specify if include a property and if to assign a custom value ( useful for complex, array types )

public static T CopyFromCustom<T>(T obj, T other, CopyFromCustomDelegate? custom = null)

Parameters

obj T
other T
custom CopyFromCustomDelegate

Returns

T

Type Parameters

T

CopyFromExclude<T>(T, T, params string[])

copy properties from other object excluding those with given names

public static T CopyFromExclude<T>(T obj, T other, params string[] exclude_names)

Parameters

obj T
other T
exclude_names string[]

Returns

T

Type Parameters

T

CopyFromInclude<T>(T, T, params string[])

copy properties from other object including only those with given names

public static T CopyFromInclude<T>(T obj, T other, params string[] include_names)

Parameters

obj T
other T
include_names string[]

Returns

T

Type Parameters

T

CopyFrom<T>(T, T, Func<PropertyInfo, bool>?)

copy properties from other object ; if match functor specified it copies only matched properties

public static T CopyFrom<T>(T obj, T other, Func<PropertyInfo, bool>? match = null)

Parameters

obj T

destination

other T

source

match Func<PropertyInfo, bool>

if specified allow to copy only matching props

Returns

T

Type Parameters

T

CreateComparer<T>(T, Comparison<T>)

Anonymous Comparer. create an IComparer from given comparison expression. first dummy argument used to infer the type T.

public static IComparer<T> CreateComparer<T>(T sample, Comparison<T> comparison)

Parameters

sample T

dummy element to infer the type T

comparison Comparison<T>

(a,b) => { -1, 0, 1 }

Returns

IComparer<T>

Type Parameters

T

DeflateEmbeddedResource(Assembly, string)

deflate embedded resource

public static Stream? DeflateEmbeddedResource(Assembly assembly, string resource)

Parameters

assembly Assembly
resource string

Returns

Stream

EnsureFolder(string)

Ensure given folder path exists.

public static string EnsureFolder(string path)

Parameters

path string

Returns

string

Exec(string, IEnumerable<string>, CancellationToken, bool, bool, bool, bool)

start a process in background redirecting standard output, error; a cancellation token can be supplied to cancel underlying process

public static Task<ExecResult> Exec(string cmd, IEnumerable<string> args, CancellationToken ct, bool sudo = false, bool redirectStdout = true, bool redirectStderr = true, bool verbose = false)

Parameters

cmd string

cmd to execute

args IEnumerable<string>

cmd arguments ( array of strings )

ct CancellationToken

cancellation token

sudo bool

true if sudo required

redirectStdout bool

redirect process stdout and grab into output

redirectStderr bool

redirect process stderr and grab into error

verbose bool

if true prints command and args used

Returns

Task<ExecResult>

Examples

\snippet exec/Program.cs example

ExecNoRedirect(string, IEnumerable<string>, CancellationToken, bool, bool)

start a process in background without redirecting standard output, error; a cancellation token can be supplied to cancel underlying process

public static Task<ExecResult> ExecNoRedirect(string cmd, IEnumerable<string> args, CancellationToken ct, bool sudo = false, bool verbose = false)

Parameters

cmd string
args IEnumerable<string>
ct CancellationToken
sudo bool
verbose bool

Returns

Task<ExecResult>

ExecRedirect(string, IEnumerable<string>, CancellationToken, bool, bool)

start a process in background redirecting standard output, error; a cancellation token can be supplied to cancel underlying process

public static Task<ExecResult> ExecRedirect(string cmd, IEnumerable<string> args, CancellationToken ct, bool sudo = false, bool verbose = false)

Parameters

cmd string
args IEnumerable<string>
ct CancellationToken
sudo bool
verbose bool

Returns

Task<ExecResult>

ExecRedirectError(string, IEnumerable<string>, CancellationToken, bool, bool)

start a process in background redirecting standard error; a cancellation token can be supplied to cancel underlying process

public static Task<ExecResult> ExecRedirectError(string cmd, IEnumerable<string> args, CancellationToken ct, bool sudo = false, bool verbose = false)

Parameters

cmd string
args IEnumerable<string>
ct CancellationToken
sudo bool
verbose bool

Returns

Task<ExecResult>

FromUnixTimeAuto(long, AllowedDateTimeOffsetRange, AllowedDateTimeOffsetRange?)

Guess if given unix time is seconds or milliseconds given a validity range of dates that must fall out of ambiguity range [1968-01-12T20:06:43.2Z, 1978-01-11T21:31:40.799Z] where tests could overlap and no decision can be assumed.

public static DateTimeOffset FromUnixTimeAuto(long value, Toolkit.AllowedDateTimeOffsetRange allowedRangeA, Toolkit.AllowedDateTimeOffsetRange? allowedRangeB = null)

Parameters

value long
allowedRangeA Toolkit.AllowedDateTimeOffsetRange
allowedRangeB Toolkit.AllowedDateTimeOffsetRange

Returns

DateTimeOffset

Remarks

If unix time is in the ambiguity range [UnixTimeAutoAmbiguityFrom,UnixTimeAutoAmbiguityTo] it can't be guessed and an exception is thrown. Out of the ambiguity it can be guessed given an allowable date ranges because these would not overlaps.
Discussion: https://stackoverflow.com/a/77809656/5521766

GetEmbeddedFileContentBytes(Assembly, string)

retrieve embedded resource file content and read into a byte array

public static byte[]? GetEmbeddedFileContentBytes(Assembly assembly, string resourceName)

Parameters

assembly Assembly

assembly that contains given resourceName

resourceName string

name of resource (eg. namespace.filename.ext)

Returns

byte[]

GetEmbeddedFileContentString(Assembly, string)

retrieve embedded resource file content and read into a string

public static string GetEmbeddedFileContentString(Assembly assembly, string resourceName)

Parameters

assembly Assembly

assembly that contains given resourceName

resourceName string

name of resource (eg. namespace.filename.ext)

Returns

string

GetEmbeddedFileContentString<T>(string)

retrieve embedded resource file content and read into a string

public static string GetEmbeddedFileContentString<T>(string resourceName) where T : class

Parameters

resourceName string

name of resource (eg. namespace.filename.ext)

Returns

string

Type Parameters

T

Type for which lookup assembly (eg. namespace.classname)

GetEmbeddedResourceNames(Assembly?)

retrieve calling assembly resource names

public static IEnumerable<string> GetEmbeddedResourceNames(Assembly? assembly = null)

Parameters

assembly Assembly

Returns

IEnumerable<string>

GetEmbeddedResourcesList<T>()

retrieve the list of embedded resource names from given Type

public static string[] GetEmbeddedResourcesList<T>() where T : class

Returns

string[]

Type Parameters

T

Type for which lookup assembly (eg. namespace.classname)

GetMemberName<T>(Expression<Func<T, object>>)

retrieve member name from a functor like `x=>x.membername1

public static string GetMemberName<T>(Expression<Func<T, object>> membersExpr)

Parameters

membersExpr Expression<Func<T, object>>

Returns

string

Type Parameters

T

GetMemberName<T>(T, Expression<Func<T, object>>)

retrieve member name from a functor like `x=>x.membername1

public static string GetMemberName<T>(T obj, Expression<Func<T, object>> membersExpr)

Parameters

obj T
membersExpr Expression<Func<T, object>>

Returns

string

Type Parameters

T

GetMemberNamesExt<T>(Expression<Func<T, object>>)

retrieve list of member names from a functor like x=>new {x.membername1, x.membername2, ...} or x=>x.membername

public static IEnumerable<string> GetMemberNamesExt<T>(Expression<Func<T, object>> membersExpr)

Parameters

membersExpr Expression<Func<T, object>>

Returns

IEnumerable<string>

Type Parameters

T

GetMemberNames<T>(Expression<Func<T, object>>)

retrieve list of member names from a functor like x=>new {x.membername1, x.membername2, ...} or x=>x.membername

public static HashSet<string> GetMemberNames<T>(Expression<Func<T, object>> membersExpr)

Parameters

membersExpr Expression<Func<T, object>>

Returns

HashSet<string>

Type Parameters

T

GetMemberNames<T>(T, Expression<Func<T, object>>)

retrieve list of member names from a functor like x=>new {x.membername1, x.membername2, ...} or x=>x.membername

public static HashSet<string> GetMemberNames<T>(T obj, Expression<Func<T, object>> membersExpr)

Parameters

obj T
membersExpr Expression<Func<T, object>>

Returns

HashSet<string>

Type Parameters

T

GetVarName<T>(Expression<Func<T>>)

retrieve name of var at runtime with GetVarName(() => variable)

public static string GetVarName<T>(Expression<Func<T>> varNameExpression)

Parameters

varNameExpression Expression<Func<T>>

Returns

string

Type Parameters

T

ParentPath(string, string)

search given stopAtFolderName directory searching backward from the given beginPathname folder

public static string? ParentPath(string beginPathname, string stopAtFolderName)

Parameters

beginPathname string
stopAtFolderName string

Returns

string

RandomPassword(RandomPasswordOptions?)

Generate random password using defaults ( allow numbers, lowercase, uppercase, 12 of length, no special symbol )

public static string RandomPassword(RandomPasswordOptions? opts = null)

Parameters

opts RandomPasswordOptions

Returns

string

Examples

\snippet random-password/Program.cs example

SaveEmbeddedResourceToFile(string, string, bool)

save given embedded resource to file

public static bool SaveEmbeddedResourceToFile(string resource, string dstPathfilename, bool deflate = false)

Parameters

resource string
dstPathfilename string
deflate bool

Returns

bool

SearchInPath(string)

Search given filename in the PATH

public static string? SearchInPath(string filename)

Parameters

filename string

Returns

string

null if not found

Swap<T>(ref T, ref T)

swap x, y values

public static void Swap<T>(ref T x, ref T y)

Parameters

x T
y T

Type Parameters

T

TailLike(string, bool, CancellationToken?, int)

Tail-like method to gather file incoming lines

public static IEnumerable<string> TailLike(string pathfilename, bool seekEnd = true, CancellationToken? ct = null, int BUFSIZE = 1024)

Parameters

pathfilename string

source pathfilename to tail

seekEnd bool

if false read from beginning (default:true)

ct CancellationToken?

optional cancellation token to control stop of tail loop

BUFSIZE int

read buffer size (default:1024)

Returns

IEnumerable<string>

return incoming lines from given file

Examples

\snippet tail-like/Program.cs example

XorNull(object, object)

returns true if only one of given objects is null; returns false if all objects null or all objects not null;

public static bool XorNull(object a, object b)

Parameters

a object
b object

Returns

bool