SandboxSystemExtensions
public static abstract sealed class SandboxSystemExtensions Methods
static this Void InvokeWithWarning(Action action) Call an action, swallow any exceptions with a warning
static this Void InvokeWithWarning(Action<T> action, T arg0) static this Void InvokeWithWarning(Action<T1, T2> action, T1 arg0, T2 arg1) static this String ToBase36(T i) Encode the given number into a Base36 string
static this Int64 FromBase36(String input) Decode the Base36 Encoded string into a number
input static this Int32 GetEpoch(DateTime d) Returns the UNIX time stamp - number of seconds since 1st of January, 1970.
d static this DateTime ToDateTime(Int32 seconds) Converts UNIX time stamp to a DateTime object.
seconds — UNIX time stamp in seconds. static this DateTime ToDateTime(Int64 seconds) static this String ToRelativeTimeString(DateTime dateTime) static this String Humanize(DateTime dateTime) Convert date into a human readable relative time string.
static this String Humanize(DateTimeOffset dateTime) Convert date into a human readable relative time string.
static this String Humanize(TimeOnly dateTime) Convert date into a human readable relative time string.
static this String Humanize(DateOnly dateTime) Convert date into a human readable relative time string.
static this String Humanize(TimeSpan dateTime, Int32 precision = 1) Convert date into a human readable relative time string.
static this TValue GetOrCreate(IDictionary<TKey, TValue> dict, TKey key) static this Dictionary<TKey, TValue> Clone(Dictionary<TKey, TValue> dict) static this Task ForEachTaskAsync(IEnumerable<T> source, Func<T, Task> body, Int32 maxRunning = 8, CancellationToken token = null) static this Type GetCommonBaseType(IEnumerable<Type> types) static this T GetPropertyValue(JsonObject jso, String membername, T defaultvalue) Get a property value by name, from a JsonObject. Return defaultValue if it's not found.
static this String FormatBytes(T input, Boolean shortFormat = False) Given a number, will format as a memory value, ie 10gb, 4mb
static this T Clamp(T input, T min, T max) Clamp a number between two values.
static this String FormatSeconds(Int64 secs) Formats the given value in format "1w2d3h4m5s". Will not display 0 values.
secs — Time to format, in seconds. static this String FormatSeconds(UInt64 secs) static this String FormatSecondsLong(Int64 secs) Formats the given value in format "4 weeks, 3 days, 2 hours and 1 minutes". Will not display 0 values. Will not display seconds if value is more than 1 hour.
secs — Time to format, in seconds. static this String FormatSecondsLong(UInt64 secs) static this String FormatNumberShort(Int64 num) "1500" becomes "1,500", "15 000" becomes "15K", "15 000 000" becomes "15KK", etc.
static this String FormatNumberShort(UInt64 num) static this Int32 UnsignedMod(Int32 a, Int32 b) Does what you expected to happen when you did "a % b", that is, handles negative values by returning a positive number from the end.
static this Int32 BitsSet(Int32 i) Returns the number of bits set in an integer. This us usually used for flags to count the amount of flags set.
static this String Plural(Int32 a, String single, String plural) Return single if 1 else plural
static this String FormatWithSuffix(Int32 num) Change 1 to 1st, 2 to 2nd etc
static this String SizeFormat(Int64 bytes) static this String SizeFormat(Int32 bytes) static this String KiloFormat(Int32 num) Format a large number into "1045M", "56K"
static this String KiloFormat(Int64 num) Format a large number into "1045M", "56K"
static this String Humanize(TimeSpan timespan, Boolean shortVersion = False, Boolean minutes = True, Boolean hours = True, Boolean days = True) Humanize a timespan into "x hours", "x seconds"
static this Boolean Contains(T value, T flag) static this T WithFlag(T value, T flag, Boolean set) static this Int32 AsInt(T value) static this String ToMetric(Int32 input, Int32 decimals = 2) Convert 1100 to 1.1k
static this Boolean IsPowerOfTwo(Int32 x) Return true if the number is a power of two (2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, etc)
static this String ToMetric(Int64 input, Int32 decimals = 2) Convert 1100 to 1.1k
static this String ToMetric(Double input, Int32 decimals = 2) Convert 1100 to 1.1k
static this String ToMetric(Single input, Int32 decimals = 2) Convert 1100 to 1.1k
static this Double Double(Random self, Double min, Double max) Returns a double between min and max
static this Single Float(Random self) Returns a random float between 0 and 1
static this Single Float(Random self, Single min, Single max) Returns a random float between min and max
static this Single Float(Random self, Single max) Returns a random float between 0 and max (or 1)
static Double Double(Random self, Double max = 1) Returns a random double between 0 and max (or 1)
static this Int32 Int(Random self, Int32 min, Int32 max) Returns a random int between min and max (inclusive)
static this Int32 Int(Random self, Int32 max) Returns a random int between 0 and max (inclusive)
static this Color Color(Random self) Returns a random Color
static this Rotation Rotation(Random self) Returns a uniformly random rotation.
static this Angles Angles(Random self) Returns the angles of a uniformly random rotation.
static this Single Gaussian(Random self, Single mean = 0, Single stdDev = 1) Sample from a Gaussian distribution with a given mean and standard deviation.
static this Vector2 Gaussian2D(Random self, Vector2? mean = null, Vector2? stdDev = null) static this Vector3 Gaussian3D(Random self, Vector3? mean = null, Vector3? stdDev = null) static this Vector4 Gaussian4D(Random self, Vector4? mean = null, Vector4? stdDev = null) static this Vector2 VectorInSquare(Random self, Single extents = 1) Uniformly samples a 2D position from a square with coordinates in the range - to +.
static this Vector3 VectorInCube(Random self, Single extents = 1) Uniformly samples a 3D position from a cube with coordinates in the range - to +.
static this Vector2 VectorInCircle(Random self, Single radius = 1) Uniformly samples a 2D position from all points with distance at most from the origin.
static this Vector3 VectorInSphere(Random self, Single radius = 1) Uniformly samples a 3D position from all points with distance at most from the origin.
static this T FromArray(Random self, T[] array, T defVal = null) Returns a random value in an array
static this T FromList(Random self, List<T> array, T defVal = null) static this Single FloatDeterministic(Random self, Int32 i) Get a random float (0-1) from a pre-calculated list. This is faster, and if you put the same seed in, it will always return the same number. The downside is that it only has 8192 variations of floats, but that seem like enough for most things.
static this T GetAttributeOfType(Enum enumVal) Gets an attribute on an enum field value
enumVal — The enum value static this Boolean IsBasedOnGenericType(Type src, Type test) Returns if this type is based on a given generic type.
src — The type to test. test — The type to test against. Typically this will be something like typeof( MyType<> ) static this Boolean CheckValidationAttributes(PropertyInfo prop, Object obj, String[] errors, String name = null) Check all ValidationAttributes on this property, and get the error messages if there are any.
prop — The property whose arguments to test. obj — Instance of the object this property is of. errors — If returned false, these will be the error messages to display. name — Override the property name in error messages. static this Boolean IsInitOnly(PropertyInfo property) Determine if this property is init-only.
property — The property to test. static this String ToSimpleString(Type type, Boolean includeNamespace = True) Returns this type's name, with nicer formatting for generic types.
static this String ToSimpleString(MemberInfo member, Boolean includeNamespace = True) Returns this member's name qualified by its declaring type, with nicer formatting for generics.
static this String ToSimpleString(Delegate deleg, Boolean includeNamespace = True) Returns a nice name for the given delegate, based on the method that implements it.
static this String ReadNullTerminatedString(Stream stream, Int64 offset) Read a null terminated string from the stream, at given offset.
stream — The stream to read from. offset — Offset where to start reading, from the beginning of the stream. static this String QuoteSafe(String str, Boolean optional = False) Puts quote marks around a string. Internal quotes are backslashed.
static this String HtmlEncode(String str) static this String UrlEncode(String str) static this String CollapseWhiteSpace(String str) Collapse sequences of whitespace into a single whitespace
str static this String CollapseSpacesAndPreserveLines(String str) Collapse sequences of spaces and tabs into a single space, preserving newlines
str static this String NormalizeFilename(String str, Boolean enforceInitialSlash = True) Puts a filename into the format /path/filename.ext (from path\FileName.EXT)
static this String NormalizeFilename(String str, Boolean enforceInitialSlash, Boolean enforceLowerCase, Char targetSeparator = /) Puts a filename into the format /path/filename.ext (from path\FileName.EXT)
static this String WithExtension(String path, String ext) Adds or replaces the extension of to .
path — A file path with or without an extension. ext — A file extension with or without a leading period. static this String SimplifyPath(String str) Gets rid of ../'s (from /path/folder/../file.txt to /path/file.txt)
str static this String[] SplitQuotesStrings(String input) in : I am "splitting a" string "because it's fun " out : ["I", "am", "splitting a", "string", "because it's fun"]
static this Single ToFloat(String str, Single Default = 0) Convert to Single, if not then return .
static this Single ToFloatEval(String expression, Single Default = 0) Convert to Single. Might be a string formula. This is always going to be slower than a call to Single).
static this Single ToDouble(String str, Single Default = 0) Convert to Double, if not then return .
static this Double ToDoubleEval(String expression, Double Default = 0) Convert to Double. Might be a string formula. This is always going to be slower than a call to Single).
static this Decimal ToDecimal(String str, Decimal Default = default) 128-bit data type that returns sane results for almost any input. All other numeric types can cast from this.
static this UInt32 ToUInt(String str, Int32 Default = 0) Convert to UInt32, if not then return .
static this Int32 ToInt(String str, Int32 Default = 0) Convert to Int32, if not then return .
static this Int32 ToIntEval(String expression, Int32 Default = 0) Convert to Int32. Might be a string formula. This is always going to be slower than a call to Int32).
static this UInt64 ToULong(String str, UInt64 Default = 0) Convert to UInt64, if not then return .
static this Int64 ToLong(String str, Int64 Default = 0) Convert to Int64, if not then return .
static this Int64 ToLongEval(String expression, Int64 Default = 0) Convert to Int64. Might be a string formula. This is always going to be slower than a call to Int64).
static this Boolean ToBool(String str) Try to convert to bool. Inputs can be true, false, yes, no, 0, 1, null (caps insensitive)
static this String Truncate(String str, Int32 maxLength, String appendage = null) If the string is longer than this amount of characters then truncate it If appendage is defined, it will be appended to the end of truncated strings (ie, "..")
static this String TruncateFilename(String str, Int32 maxLength, String appendage = null) If the string is longer than this amount of characters then truncate it If appendage is defined, it will be appended to the end of truncated strings (ie, "..")
static this Boolean Contains(String source, String toCheck, StringComparison comp) An extended Contains which takes a StringComparison.
static this String Snippet(String source, String find, Int32 padding) Given a large string, find all occurrences of a substring and return them with padding. This is useful in situations where you're searching for a word in a hug body of text, and want to show how it's used without displaying the whole text.
static this String ToTitleCase(String source) Convert a variable name to something more user friendly.
static this String RemoveBadCharacters(String str) Removes bad, invisible characters that are commonly used to exploit. https://en.wikipedia.org/wiki/Zero-width_non-joiner
static this String Base64Encode(String plainText) Convert to a base64 encoded string
static this String Base64Decode(String base64EncodedData) Convert from a base64 encoded string
static this Object ToType(String str, Type t) Try to politely convert from a string to another type
static this Boolean TryToType(String str, Type t, Object Value) Try to politely convert from a string to another type
static this Int32 FastHash(String str) Generate a 32-bit truncated xxhash3 hash from given string.
static this UInt64 FastHash64(String str) Generate a 64-bit xxhash3 hash from given string.
static this Guid ToGuid(String input) Take a string and create a deterministic guid.
static this String Md5(String input) Get the md5 hash of a string.
static this String Columnize(String str, Int32 maxLength, Boolean right = False) convert "string" into "string " or " string"
static this Boolean WildcardMatch(String str, String wildcard) Returns true if this string matches a wildcard match. Check is case insensitive.
static this String TrimQuoted(String str, Boolean ignoreSurroundingSpaces = False) The string might start and end in quotes ( ", ' ), remove those if that is the case.
static this Int32 Distance(String source, String target) Return the distance between two strings. Useful for ordering strings by similarity
static this Boolean IsValidTag(String source) Is this string a valid Tag. This is a way to check if a string is a valid tag, project wide. So our logic is always the same. - not null - between 1 and 32 chars - a-z
static this String GetFilenameSafe(String input) Make the passed in string filename safe. This replaces any invalid characters with "_".
static this String ToRelativeTimeString(TimeSpan span) static this String ToRemainingTimeString(TimeSpan span) static this Int32 GetManagedSize(Type t) Get the managed size of a given type. This matches an IL-level sizeof(t), even if it cannot be determined normally in C#. Note that sizeof(t) != Marshal.SizeOf(t) when t is char or bool.
static this IEnumerable<Vector3> CatmullRomSpline(IEnumerable<Vector3> points, Int32 interpolation) static this IEnumerable<Vector3> TcbSpline(IEnumerable<Vector3> points, Int32 interpolation, Single tension, Single continuity, Single bias) static this Boolean IsValid(IValid obj) Returns false if IValid object is null or if IsValid returns false.