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
UnixTimeAutoAmbiguityTo
public static readonly DateTimeOffset UnixTimeAutoAmbiguityTo
Field Value
Methods
AppDataFolder(string)
{AppData}/{namespace}/{assembly_name}
public static string AppDataFolder(string ns)
Parameters
nsstring
Returns
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
srcTdstTexcludeFunc<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
objTotherTcustomCopyFromCustomDelegate
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
objTotherTexclude_namesstring[]
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
objTotherTinclude_namesstring[]
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
objTdestination
otherTsource
matchFunc<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
sampleTdummy element to infer the type T
comparisonComparison<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
Returns
EnsureFolder(string)
Ensure given folder path exists.
public static string EnsureFolder(string path)
Parameters
pathstring
Returns
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
cmdstringcmd to execute
argsIEnumerable<string>cmd arguments ( array of strings )
ctCancellationTokencancellation token
sudobooltrue if sudo required
redirectStdoutboolredirect process stdout and grab into output
redirectStderrboolredirect process stderr and grab into error
verboseboolif true prints command and args used
Returns
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
cmdstringargsIEnumerable<string>ctCancellationTokensudoboolverbosebool
Returns
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
cmdstringargsIEnumerable<string>ctCancellationTokensudoboolverbosebool
Returns
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
cmdstringargsIEnumerable<string>ctCancellationTokensudoboolverbosebool
Returns
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
valuelongallowedRangeAToolkit.AllowedDateTimeOffsetRangeallowedRangeBToolkit.AllowedDateTimeOffsetRange
Returns
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
assemblyAssemblyassembly that contains given resourceName
resourceNamestringname 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
assemblyAssemblyassembly that contains given resourceName
resourceNamestringname of resource (eg. namespace.filename.ext)
Returns
GetEmbeddedFileContentString<T>(string)
retrieve embedded resource file content and read into a string
public static string GetEmbeddedFileContentString<T>(string resourceName) where T : class
Parameters
resourceNamestringname of resource (eg. namespace.filename.ext)
Returns
Type Parameters
TType for which lookup assembly (eg. namespace.classname)
GetEmbeddedResourceNames(Assembly?)
retrieve calling assembly resource names
public static IEnumerable<string> GetEmbeddedResourceNames(Assembly? assembly = null)
Parameters
assemblyAssembly
Returns
GetEmbeddedResourcesList<T>()
retrieve the list of embedded resource names from given Type
public static string[] GetEmbeddedResourcesList<T>() where T : class
Returns
- string[]
Type Parameters
TType 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
membersExprExpression<Func<T, object>>
Returns
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
objTmembersExprExpression<Func<T, object>>
Returns
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
membersExprExpression<Func<T, object>>
Returns
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
membersExprExpression<Func<T, object>>
Returns
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
objTmembersExprExpression<Func<T, object>>
Returns
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
varNameExpressionExpression<Func<T>>
Returns
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
Returns
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
Returns
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
Returns
SearchInPath(string)
Search given filename in the PATH
public static string? SearchInPath(string filename)
Parameters
filenamestring
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
xTyT
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
pathfilenamestringsource pathfilename to tail
seekEndboolif false read from beginning (default:true)
ctCancellationToken?optional cancellation token to control stop of tail loop
BUFSIZEintread 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)