Skip to content

Instantly share code, notes, and snippets.

@FWest98
Last active August 29, 2015 14:13
Show Gist options
  • Save FWest98/1dc43251d0e5a457cfd9 to your computer and use it in GitHub Desktop.
Save FWest98/1dc43251d0e5a457cfd9 to your computer and use it in GitHub Desktop.
Dynamic WinForm Control invoker
using System;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
namespace System.Windows.Forms {
public static class DynamicInvokers {
// This would replace a Control.Property = value
private delegate void SetPropertyCallBack(Control c, string property, dynamic value);
/// <summary>
/// Set a property on a control from any thread. This automatically invokes if necessary.
/// </summary>
/// <param name="c">Control</param>
/// <param name="property">The name of the property to be set</param>
/// <param name="value">The value to set to the property</param>
public static void SetProperty(this Control c, string property, dynamic value) {
if (c.GetType().GetProperty(property) == null) return;
if (c.InvokeRequired) {
c.Invoke(new SetPropertyCallBack(SetProperty), c, property, value);
} else {
c.GetType().GetProperty(property).SetValue(c, value, null);
}
}
// This would replace a Control.Method(args)-call
private delegate void RunMethodCallback(Control c, string name, params dynamic[] args);
/// <summary>
/// Call a method on a control from any thread. This automatically invokes if necessary.
/// </summary>
/// <param name="c">The control to call the method on</param>
/// <param name="name">The name of the method to call</param>
/// <param name="args">Optional arguments to pass to the method, in the same order</param>
public static void RunMethod(this Control c, string name, params dynamic[] args) {
if (c.GetType().GetMethod(name) == null) return;
if (args == null || args.Length == 0) args = null;
if (c.InvokeRequired) {
c.Invoke(new RunMethodCallback(RunMethod), c, name, args);
} else {
c.GetType().GetMethod(name).Invoke(c, args);
}
}
// This would replace a Control.Property.Method(args)-call
private delegate void RunMethodOnPropertyCallback(Control c, string property, string func, params dynamic[] args);
/// <summary>
/// Call a method on a property of a control from any thread. This automatically invokes if necessary.
/// </summary>
/// <param name="c">The control to get the property from</param>
/// <param name="property">The name of the property</param>
/// <param name="func">The name of the method to call</param>
/// <param name="args">Optional arguments to pass to the method, in the same order</param>
public static void RunMethodOnProperty(this Control c, string property, string func, params dynamic[] args) {
var types = args == null ? new Type[]{} : args.Select(s => s.GetType() as Type).ToArray();
if (c.GetType().GetProperty(property) == null || c.GetType().GetProperty(property).PropertyType.GetMethod(func, types) == null) return;
if (args == null || args.Length == 0) args = null;
if (c.InvokeRequired) {
c.Invoke(new RunMethodOnPropertyCallback(RunMethodOnProperty), c, property, func, args);
} else {
c.GetType().GetProperty(property).PropertyType.GetMethod(func, types).Invoke(c.GetType().GetProperty(property).GetValue(c), args);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment