From e7b559f1dad01cf5da2b788b39d799d09dd57604 Mon Sep 17 00:00:00 2001 From: noisyfox Date: Wed, 23 Nov 2016 02:42:12 +1100 Subject: [PATCH] Refine code --- .../System/Hotkeys/HotkeyCallbacks.cs | 23 +++++++++++++++++-- shadowsocks-csharp/Program.cs | 2 +- shadowsocks-csharp/View/HotkeySettingsForm.cs | 16 +------------ 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/Hotkeys/HotkeyCallbacks.cs b/shadowsocks-csharp/Controller/System/Hotkeys/HotkeyCallbacks.cs index de811e57..2f51f4cb 100644 --- a/shadowsocks-csharp/Controller/System/Hotkeys/HotkeyCallbacks.cs +++ b/shadowsocks-csharp/Controller/System/Hotkeys/HotkeyCallbacks.cs @@ -1,11 +1,11 @@ -using System.Reflection; +using System; +using System.Reflection; using Shadowsocks.View; namespace Shadowsocks.Controller.Hotkeys { public class HotkeyCallbacks { - public static HotkeyCallbacks Instance { get; private set; } public static void InitInstance(ShadowsocksController controller) { @@ -17,6 +17,23 @@ namespace Shadowsocks.Controller.Hotkeys Instance = new HotkeyCallbacks(controller); } + /// + /// Create hotkey callback handler delegate based on callback name + /// + /// + /// + public static Delegate GetCallback(string methodname) + { + if (methodname.IsNullOrEmpty()) throw new ArgumentException(nameof(methodname)); + MethodInfo dynMethod = typeof(HotkeyCallbacks).GetMethod(methodname, + BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase); + return dynMethod == null ? null : Delegate.CreateDelegate(typeof(HotKeys.HotKeyCallBackHandler), Instance, dynMethod); + } + + #region Singleton + + private static HotkeyCallbacks Instance { get; set; } + private readonly ShadowsocksController _controller; private HotkeyCallbacks(ShadowsocksController controller) @@ -24,6 +41,8 @@ namespace Shadowsocks.Controller.Hotkeys _controller = controller; } + #endregion + #region Callbacks private void SwitchSystemProxyCallback() diff --git a/shadowsocks-csharp/Program.cs b/shadowsocks-csharp/Program.cs index c89fb689..e8fe6db8 100755 --- a/shadowsocks-csharp/Program.cs +++ b/shadowsocks-csharp/Program.cs @@ -82,7 +82,7 @@ namespace Shadowsocks #endif _controller = new ShadowsocksController(); _viewController = new MenuViewController(_controller); - HotKeys.Init(); + HotKeys.Init(_controller); _controller.Start(); Application.Run(); } diff --git a/shadowsocks-csharp/View/HotkeySettingsForm.cs b/shadowsocks-csharp/View/HotkeySettingsForm.cs index 5b6abcdb..8e6b8c15 100644 --- a/shadowsocks-csharp/View/HotkeySettingsForm.cs +++ b/shadowsocks-csharp/View/HotkeySettingsForm.cs @@ -270,7 +270,7 @@ namespace Shadowsocks.View var labelName = rawName + "Label"; var callbackName = rawName + "Callback"; - var callback = GetDelegateViaMethodName(callbackName); + var callback = HotkeyCallbacks.GetCallback(callbackName); if (callback == null) { throw new Exception($"{callbackName} not found"); @@ -302,20 +302,6 @@ namespace Shadowsocks.View return fi == null ? null : fi.GetValue(obj); } - /// - /// Create hotkey callback handler delegate based on callback name - /// - /// - /// - /// - private Delegate GetDelegateViaMethodName(string methodname) - { - if (methodname.IsNullOrEmpty()) throw new ArgumentException(nameof(methodname)); - MethodInfo dynMethod = typeof(HotkeyCallbacks).GetMethod(methodname, - BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase); - return dynMethod == null ? null : Delegate.CreateDelegate(typeof(HotKeys.HotKeyCallBackHandler), HotkeyCallbacks.Instance, dynMethod); - } - #endregion } } \ No newline at end of file