Added generic methods for one-liners

This commit is contained in:
CreepyCrafter24 2019-11-29 21:42:39 +01:00
parent c95f2b8584
commit 9d058ede6d
6 changed files with 166 additions and 18 deletions

View File

@ -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
{

79
Misc/Forms.cs Normal file
View File

@ -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, TProp>(TCtl control, Expression<Func<TCtl, TProp>> propexpr, TProp value) where TCtl : Control;
public delegate TProp GetPropertyDelegate<TCtl, TProp>(TCtl control, Expression<Func<TProp>> propexpr) where TCtl : Control;
public delegate void InvokeActionDelegate<TCtl>(TCtl control, Delegate dlg, params object[] args) where TCtl : Control;
public delegate TResult InvokeFuncDelegate<TCtl, TResult>(TCtl control, Delegate dlg, params object[] args) where TCtl : Control;
public static class Forms
{
public static void SetProperty<TCtl, TProp>(this TCtl control, Expression<Func<TCtl, TProp>> 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<TCtl, TProp>(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<TCtl, TProp>(this TCtl control, Expression<Func<TProp>> 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<TCtl, TProp>(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<TCtl>(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<TCtl>(InvokeAction), control, dlg, args);
return;
}
dlg.DynamicInvoke(args);
}
public static TResult InvokeFunc<TCtl, TResult>(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<TCtl, TResult>(InvokeFunc<TCtl, TResult>), control, dlg, args) : (TResult)dlg.DynamicInvoke(args);
}
}
}

60
Misc/GenericExtensions.cs Normal file
View File

@ -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<G, T>(this Dictionary<G, T> dict, G key, T def)
{
if (!dict.ContainsKey(key))
dict[key] = def;
return dict[key];
}
public static T set<G, T>(this Dictionary<G, T> dict, G key, T val)
{
dict[key] = val;
return dict[key];
}
public static bool tryCast<T>(this object o, out T parsed)
{
try
{
parsed = (T)o;
return true;
}
catch
{
parsed = default(T);
return false;
}
}
public static G selectO<T, G>(this T self, Func<T, G> func) => func.Invoke(self);
public static void runIf(bool condition, Action func)
{
if (condition)
func();
}
public static T ParseToEnum<T>(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;
}
}

22
Misc/IO.cs Normal file
View File

@ -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; }
}
}
}

View File

@ -46,7 +46,10 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ArrayFormatter.cs" />
<Compile Include="Forms.cs" />
<Compile Include="GenericExtensions.cs" />
<Compile Include="HID.cs" />
<Compile Include="IO.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security.cs" />
</ItemGroup>

View File

@ -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; }
}
}
}