Skip to content

Extension Methods

Nill edited this page Oct 26, 2021 · 2 revisions

The Telefrag namespace comes with a plethora of extension methods you may find extremely useful in tandem with Telefrag development. Some of these methods are described below:

Object

Method Signature Description
object.AssertParam(string name) Throws a NullArgumentException if the object is null using the parameter name provided.
object.ToJSON(settings?) Serializes the object to JSON using the provided settings, or the value of Json.Settings if none are provided.
object.Dump() Same as ToJSON() but uses indented formatting by default.

Enum

Method Signature Description
Enum.Description() Gets the value of the [Description] attribute; useful instead of ToString() for enums when you have value names that need spaces or special characters.

Net.IPAddress

Method Signature Description
IPAddress.IsPrivate() Returns true if the IP address is private (RFC1918 or loopback for IPv4; link-local, site-local, or loopback for IPv6)
IPAddress.IsLoopback() Returns true if the IP address is a loopback address

Int

Method Signature Description
int.AreIs() Returns "are" or "is" depending on how many there are/is.

Byte[]

Method Signature Description
byte[].ToUnicodeString() Returns the byte array as a Unicode string
byte[].ToASCIIString() Returns the byte array as an ASCII encoded string
byte[].ToUTF8String() Returns the byte array as a UTF-8 encoded string
byte[].ToHex() Returns a the byte array in hex format

Long and Long?

Method Signature Description
long.ToArgument() Turns a long into an Argument
long?.ToArgument() Turns a long? into an Argument
long.TimeNice() Interprets the long as UNIX time and outputs a friendly string such as Today at 1:19 PM
long.ToDate() Converts a UNIX timestamp into a DateTime

String

Method Signature Description
string.ToArgs(char delim = ' ') Returns an ArgumentsCollection by splitting the string
string.ToByteArray(Encoding encoding = null) Turns a string into a byte array using the specified (or default) encoding
string.Is(string s) Case-insensitive culture-invariant comparison between two strings
string.Is(Argument a) Case-insensitive culture-invariant comparison between string and Argument
string.Safe() Escapes a string being parsed as HTML such that any HTML characters are HTML-encoded, but only as required by Telegram
string.HTMLEncode() Unfiltered HTML entity-encoding of text
string.HTMLDecode() Unfiltered HTML entity-decoding of text
string.JsEscape() Javascript-escape a string
string.UrlEncode() URL-Encodes a string using % marks
string.UrlDecode() URL-Decodes a string
string.FromJSON<T>(settings?) Deserializes the string to an object of type T
string.Like(string pattern) Performs a wildcard (glob) search between two strings
string.RemoveWhitespace() Trims the string and replaces all consecutive whitespace with single spaces
string.Nill() Shortcut for string.IsNullOrEmpty(x)
string.There() Opposite of Nill, the string is non-null and non-empty.
string.Regex(pattern, g1, ..., m, options) Many overloads, performs a regular expression match, and outputs the values of the capturing groups to any of the out parameters provided, as well as the Match in the last out parameter.
string.RegexGet(pattern, groupNum, options) Performs a regex and gets the value of a capture group by index
string.RegexGetInt(pattern, groupNum, options) Performs a regex and gets the value of a capture group by index parsed as an integer
string.RegexTimeout(pattern, options, timeout) Performs a regex match with an optional timeout
string.RegexMatches(pattern, options) Simple boolean regex match
string.RegexMatch(pattern, options) Regex match and returns the Match object
string.RegexReplace(pattern, replacement, options) Evaluates a regex replacement
string.ToLong() Parses string to a long (invalid parse returns 0)
string.ToLongMaybe() Parses a string to a long? (invalid parse returns null)
string.ToInt() Parses a string to an int
string.ToIntMaybe() Parses a string to an int?
string.GetToken(int number, char delim, bool toEnd) Gets the specified token from the input string when split using the delimiter specified (default is ' '). toEnd returns everything from that token through the end of the string
string.MD5() Calculates the MD5 hash of the string
string.SHA1() Calculates the SHA1 hash of the string
string.SHA256() Calculates the SHA256 hash of the string
string.IsComplexPassword(int classes) Determines if the string is complex enough given the required number (of 4 total) character classes desired (uppercase, lowercase, numbers, symbols). Does not check password for length.
string.InstancesOf(char) Counts how many instances of the character appear in the string
string.ToBoolOr<T> Takes an input string that is either "true" or a JSON object and returns a tuple of (bool ok, T value)

String[]

Method Signature Description
string[].Has(string s) Tests to see if any of the strings in the string array are s in a case-sensitive culture-invariant manner

Type

Method Signature Description
Type.IsAssignableTo(Type t) Inverse of Type.IsAssignableFrom()
Type.GetFriendlyTypeName() Returns the C# style format of the type (e.g. Name<T> instead of Name+T\1`
Type.AttribName() Gets the human-readable type name, removing the Attribute suffix if it has one.
Type.Create(args ...) Attempts to create an instance of the type.
Type.Create\<T\>(args..) Same as Create but strongly-typed generic

IEnumerable<T>

Method Signature Description
IE<T>.Bucketize(int i) Groups the objects into bins of size i

DateTime

Method Signature Description
DateTime.ToLong() Converts the date into UNIX timestamp format
Clone this wiki locally