diff --git a/Misc/ArrayFormatter.cs b/Misc/ArrayFormatter.cs index 37e9a8a..399b002 100644 --- a/Misc/ArrayFormatter.cs +++ b/Misc/ArrayFormatter.cs @@ -2,7 +2,7 @@ { public static class ArrayFormatter { - public static string ArrayToString(object[] Input, string Seperator = "\r\n") + public static string ElementsToString(this object[] Input, string Seperator = "\r\n") { try { diff --git a/Misc/Forms.cs b/Misc/Forms.cs new file mode 100644 index 0000000..6671c89 --- /dev/null +++ b/Misc/Forms.cs @@ -0,0 +1,79 @@ +using System; +using System.Linq.Expressions; +using System.Reflection; +using System.Windows.Forms; + +namespace CC_Functions.Misc +{ + public delegate void SetPropertyDelegate(TCtl control, Expression> propexpr, TProp value) where TCtl : Control; + + public delegate TProp GetPropertyDelegate(TCtl control, Expression> propexpr) where TCtl : Control; + + public delegate void InvokeActionDelegate(TCtl control, Delegate dlg, params object[] args) where TCtl : Control; + + public delegate TResult InvokeFuncDelegate(TCtl control, Delegate dlg, params object[] args) where TCtl : Control; + + public static class Forms + { + public static void SetProperty(this TCtl control, Expression> propexpr, TProp value) where TCtl : Control + { + if (control == null) + throw new ArgumentNullException(nameof(control)); + + if (propexpr == null) + throw new ArgumentNullException(nameof(propexpr)); + if (control.InvokeRequired) + { + control.Invoke(new SetPropertyDelegate(SetProperty), control, propexpr, value); + return; + } + var propexprm = propexpr.Body as MemberExpression; + if (propexprm == null) + throw new ArgumentException("Invalid member expression.", nameof(propexpr)); + var prop = propexprm.Member as PropertyInfo; + if (prop == null) + throw new ArgumentException("Invalid property supplied.", nameof(propexpr)); + prop.SetValue(control, value); + } + + public static TProp GetProperty(this TCtl control, Expression> propexpr) where TCtl : Control + { + if (control == null) + throw new ArgumentNullException(nameof(control)); + if (propexpr == null) + throw new ArgumentNullException(nameof(propexpr)); + if (control.InvokeRequired) + return (TProp)control.Invoke(new GetPropertyDelegate(GetProperty), control, propexpr); + var propexprm = propexpr.Body as MemberExpression; + if (propexprm == null) + throw new ArgumentException("Invalid member expression.", nameof(propexpr)); + var prop = propexprm.Member as PropertyInfo; + if (prop == null) + throw new ArgumentException("Invalid property supplied.", nameof(propexpr)); + return (TProp)prop.GetValue(control); + } + + public static void InvokeAction(this TCtl control, Delegate dlg, params object[] args) where TCtl : Control + { + if (control == null) + throw new ArgumentNullException(nameof(control)); + if (dlg == null) + throw new ArgumentNullException(nameof(dlg)); + if (control.InvokeRequired) + { + control.Invoke(new InvokeActionDelegate(InvokeAction), control, dlg, args); + return; + } + dlg.DynamicInvoke(args); + } + + public static TResult InvokeFunc(this TCtl control, Delegate dlg, params object[] args) where TCtl : Control + { + if (control == null) + throw new ArgumentNullException(nameof(control)); + if (dlg == null) + throw new ArgumentNullException(nameof(dlg)); + return control.InvokeRequired ? (TResult)control.Invoke(new InvokeFuncDelegate(InvokeFunc), control, dlg, args) : (TResult)dlg.DynamicInvoke(args); + } + } +} \ No newline at end of file diff --git a/Misc/GenericExtensions.cs b/Misc/GenericExtensions.cs new file mode 100644 index 0000000..c0dbdc7 --- /dev/null +++ b/Misc/GenericExtensions.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace CC_Functions.Misc +{ + public static class GenericExtensions + { + public static T get(this Dictionary dict, G key, T def) + { + if (!dict.ContainsKey(key)) + dict[key] = def; + return dict[key]; + } + + public static T set(this Dictionary dict, G key, T val) + { + dict[key] = val; + return dict[key]; + } + + public static bool tryCast(this object o, out T parsed) + { + try + { + parsed = (T)o; + return true; + } + catch + { + parsed = default(T); + return false; + } + } + + public static G selectO(this T self, Func func) => func.Invoke(self); + + public static void runIf(bool condition, Action func) + { + if (condition) + func(); + } + + public static T ParseToEnum(string value) => (T)Enum.Parse(typeof(T), Enum.GetNames(typeof(T)).First(s => s.ToLower() == value.ToLower())); + + public static bool? ParseBool(string value) => (string.IsNullOrWhiteSpace(value) || value.ToLower() == "Indeterminate") ? (bool?)null : bool.Parse(value); + + public static bool AND(this bool? left, bool? right) => left.TRUE() && right.TRUE(); + + public static bool OR(this bool? left, bool? right) => left.TRUE() || right.TRUE(); + + public static bool XOR(this bool? left, bool? right) => left.OR(right) && (!left.AND(right)); + + public static bool TRUE(this bool? self) => self == true; + + public static bool FALSE(this bool? self) => self == false; + + public static bool NULL(this bool? self) => self == null; + } +} \ No newline at end of file diff --git a/Misc/IO.cs b/Misc/IO.cs new file mode 100644 index 0000000..e66f147 --- /dev/null +++ b/Misc/IO.cs @@ -0,0 +1,22 @@ +using System.IO; + +namespace CC_Functions.Misc +{ + public static class IO + { + public static long GetDirectorySize(string path) + { + try + { + string[] a = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); + long size = 0; + for (int i = 0; i < a.Length; i++) + { + size += new FileInfo(a[i]).Length; + } + return size; + } + catch { throw; } + } + } +} \ No newline at end of file diff --git a/Misc/Misc.csproj b/Misc/Misc.csproj index 9ea5449..e157d72 100644 --- a/Misc/Misc.csproj +++ b/Misc/Misc.csproj @@ -46,7 +46,10 @@ + + + diff --git a/Misc/Security.cs b/Misc/Security.cs index 8b0ba0f..bffe44b 100644 --- a/Misc/Security.cs +++ b/Misc/Security.cs @@ -1,25 +1,9 @@ -using System.IO; -using System.Security.Principal; +using System.Security.Principal; namespace Misc { public static class MiscFunctions { public static bool IsAdministrator => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator); - - public static long GetDirectorySize(string path) - { - try - { - string[] a = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); - long size = 0; - for (int i = 0; i < a.Length; i++) - { - size += new FileInfo(a[i]).Length; - } - return size; - } - catch { throw; } - } } } \ No newline at end of file