This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
CC-Functions/Misc/Forms.cs

92 lines
3.8 KiB
C#
Raw Normal View History

2019-11-29 21:42:39 +01:00
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Windows.Forms;
namespace CC_Functions.Misc
{
2019-12-22 16:58:16 +01:00
public delegate void SetPropertyDelegate<TCtl, TProp>(TCtl control, Expression<Func<TCtl, TProp>> propexpr,
TProp value) where TCtl : Control;
2019-11-29 21:42:39 +01:00
2019-12-22 16:58:16 +01:00
public delegate TProp GetPropertyDelegate<TCtl, TProp>(TCtl control, Expression<Func<TProp>> propexpr)
where TCtl : Control;
2019-11-29 21:42:39 +01:00
2019-12-22 16:58:16 +01:00
public delegate void InvokeActionDelegate<TCtl>(TCtl control, Delegate dlg, params object[] args)
where TCtl : Control;
2019-11-29 21:42:39 +01:00
2019-12-22 16:58:16 +01:00
public delegate TResult InvokeFuncDelegate<TCtl, TResult>(TCtl control, Delegate dlg, params object[] args)
where TCtl : Control;
2019-11-29 21:42:39 +01:00
public static class Forms
{
2019-12-22 16:58:16 +01:00
public static void SetProperty<TCtl, TProp>(this TCtl control, Expression<Func<TCtl, TProp>> propexpr,
TProp value) where TCtl : Control
2019-11-29 21:42:39 +01:00
{
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;
}
2019-12-22 16:58:16 +01:00
MemberExpression propexprm = propexpr.Body as MemberExpression;
2019-11-29 21:42:39 +01:00
if (propexprm == null)
throw new ArgumentException("Invalid member expression.", nameof(propexpr));
2019-12-22 16:58:16 +01:00
PropertyInfo prop = propexprm.Member as PropertyInfo;
2019-11-29 21:42:39 +01:00
if (prop == null)
throw new ArgumentException("Invalid property supplied.", nameof(propexpr));
prop.SetValue(control, value);
}
2019-12-22 16:58:16 +01:00
public static TProp GetProperty<TCtl, TProp>(this TCtl control, Expression<Func<TProp>> propexpr)
where TCtl : Control
2019-11-29 21:42:39 +01:00
{
if (control == null)
throw new ArgumentNullException(nameof(control));
if (propexpr == null)
throw new ArgumentNullException(nameof(propexpr));
if (control.InvokeRequired)
2019-12-22 16:58:16 +01:00
return (TProp) control.Invoke(new GetPropertyDelegate<TCtl, TProp>(GetProperty), control, propexpr);
MemberExpression propexprm = propexpr.Body as MemberExpression;
2019-11-29 21:42:39 +01:00
if (propexprm == null)
throw new ArgumentException("Invalid member expression.", nameof(propexpr));
2019-12-22 16:58:16 +01:00
PropertyInfo prop = propexprm.Member as PropertyInfo;
2019-11-29 21:42:39 +01:00
if (prop == null)
throw new ArgumentException("Invalid property supplied.", nameof(propexpr));
2019-12-22 16:58:16 +01:00
return (TProp) prop.GetValue(control);
2019-11-29 21:42:39 +01:00
}
2019-12-22 16:58:16 +01:00
public static void InvokeAction<TCtl>(this TCtl control, Delegate dlg, params object[] args)
where TCtl : Control
2019-11-29 21:42:39 +01:00
{
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;
}
2019-12-22 16:58:16 +01:00
2019-11-29 21:42:39 +01:00
dlg.DynamicInvoke(args);
}
2019-12-22 16:58:16 +01:00
public static TResult InvokeFunc<TCtl, TResult>(this TCtl control, Delegate dlg, params object[] args)
where TCtl : Control
2019-11-29 21:42:39 +01:00
{
if (control == null)
throw new ArgumentNullException(nameof(control));
if (dlg == null)
throw new ArgumentNullException(nameof(dlg));
2019-12-22 16:58:16 +01:00
return control.InvokeRequired
? (TResult) control.Invoke(new InvokeFuncDelegate<TCtl, TResult>(InvokeFunc<TCtl, TResult>), control,
dlg, args)
: (TResult) dlg.DynamicInvoke(args);
2019-11-29 21:42:39 +01:00
}
}
}