diff --git a/VAR.Toolbox/Code/BaseFactory.cs b/VAR.Toolbox/Code/BaseFactory.cs new file mode 100644 index 0000000..41eee61 --- /dev/null +++ b/VAR.Toolbox/Code/BaseFactory.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace VAR.Toolbox.Code +{ + public abstract class BaseFactory where T : INamed + { + private static Dictionary _dictTypes = null; + + private static Dictionary GetDict() + { + if (_dictTypes != null) + { + return _dictTypes; + } + + Type iType = typeof(T); + IEnumerable types = ReflectionUtils.GetTypesOfInterface(iType); + _dictTypes = types.ToDictionary(t => + { + T type = (T)System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t); + return type.Name; + }); + + return _dictTypes; + } + + public static string[] GetNames() + { + Dictionary dict = GetDict(); + return dict.Select(p => p.Key).ToArray(); + } + + public static T CreateFromName(string name) + { + Dictionary dict = GetDict(); + if (dict.ContainsKey(name) == false) + { + throw new NotImplementedException(string.Format("Cant create {1} with this name: {0}", name, typeof(T).Name)); + } + Type type = dict[name]; + + T instance = (T)Activator.CreateInstance(type); + return instance; + } + + public static T CreateFromConfig(string config) + { + Dictionary dict = GetDict(); + int indexOfColon = config.IndexOf(':'); + string name = config.Substring(0, indexOfColon < 0 ? config.Length : indexOfColon); + string nextConfig = config.Substring(indexOfColon + 1); + if (string.IsNullOrEmpty(name)) + { + return (T)(object)null; + } + if (dict.ContainsKey(name) == false) + { + throw new NotImplementedException(string.Format("Cant create {1} with this config: {0}", config, typeof(T).Name)); + } + Type type = dict[name]; + + T instance = (T)Activator.CreateInstance(type, new object[] { nextConfig }); + return instance; + } + } + + public interface INamed + { + string Name { get; } + } +} diff --git a/VAR.Toolbox/Code/ProxyCmdExecutors/IProxyCmdExecutor.cs b/VAR.Toolbox/Code/ProxyCmdExecutors/IProxyCmdExecutor.cs index ee2f697..86dc772 100644 --- a/VAR.Toolbox/Code/ProxyCmdExecutors/IProxyCmdExecutor.cs +++ b/VAR.Toolbox/Code/ProxyCmdExecutors/IProxyCmdExecutor.cs @@ -1,9 +1,7 @@ namespace VAR.Toolbox.Code.ProxyCmdExecutors { - public interface IProxyCmdExecutor + public interface IProxyCmdExecutor : INamed { - string Name { get; } - bool ExecuteCmd(string cmd, IOutputHandler outputHandler); } } diff --git a/VAR.Toolbox/Code/ProxyCmdExecutors/ProxyCmdExecutorFactory.cs b/VAR.Toolbox/Code/ProxyCmdExecutors/ProxyCmdExecutorFactory.cs index fcdba5a..26637a1 100644 --- a/VAR.Toolbox/Code/ProxyCmdExecutors/ProxyCmdExecutorFactory.cs +++ b/VAR.Toolbox/Code/ProxyCmdExecutors/ProxyCmdExecutorFactory.cs @@ -1,50 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace VAR.Toolbox.Code.ProxyCmdExecutors +namespace VAR.Toolbox.Code.ProxyCmdExecutors { - public class ProxyCmdExecutorFactory + public class ProxyCmdExecutorFactory : BaseFactory { - private static Dictionary _dictProxyCmdExecutors = null; - - private static Dictionary GetDict() - { - if (_dictProxyCmdExecutors != null) - { - return _dictProxyCmdExecutors; - } - - Type iTextCoder = typeof(IProxyCmdExecutor); - IEnumerable toolFormTypes = ReflectionUtils.GetTypesOfInterface(iTextCoder); - _dictProxyCmdExecutors = toolFormTypes.ToDictionary(t => - { - IProxyCmdExecutor proxyCmdExecutor = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as IProxyCmdExecutor; - return proxyCmdExecutor.Name; - }); - - return _dictProxyCmdExecutors; - } - - public static IProxyCmdExecutor CreateFromConfig(string config) - { - Dictionary dict = GetDict(); - int indexOfColon = config.IndexOf(':'); - string name = config.Substring(0, indexOfColon < 0 ? config.Length : indexOfColon); - string nextConfig = config.Substring(indexOfColon + 1); - if (string.IsNullOrEmpty(name)) - { - return new ProxyCmdExecutorDummy(string.Empty); - } - if (dict.ContainsKey(name) == false) - { - throw new NotImplementedException(string.Format("Cant create IProxyCmdExecutor with this config: {0}", config)); - } - Type proxyCmdExecutorType = dict[name]; - - IProxyCmdExecutor proxyCmdExecutor = Activator.CreateInstance(proxyCmdExecutorType, new object[] { nextConfig }) as IProxyCmdExecutor; - - return proxyCmdExecutor; - } } } diff --git a/VAR.Toolbox/Code/TextCoders/ITextCoder.cs b/VAR.Toolbox/Code/TextCoders/ITextCoder.cs index 02d9ae1..a316b86 100644 --- a/VAR.Toolbox/Code/TextCoders/ITextCoder.cs +++ b/VAR.Toolbox/Code/TextCoders/ITextCoder.cs @@ -1,9 +1,7 @@ namespace VAR.Toolbox.Code.TextCoders { - public interface ITextCoder + public interface ITextCoder : INamed { - string Name { get; } - bool NeedsKey { get; } string Encode(string input, string key); diff --git a/VAR.Toolbox/Code/TextCoders/TextCoderFactory.cs b/VAR.Toolbox/Code/TextCoders/TextCoderFactory.cs index fc36129..5404e7b 100644 --- a/VAR.Toolbox/Code/TextCoders/TextCoderFactory.cs +++ b/VAR.Toolbox/Code/TextCoders/TextCoderFactory.cs @@ -1,46 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace VAR.Toolbox.Code.TextCoders +namespace VAR.Toolbox.Code.TextCoders { - public class TextCoderFactory + public class TextCoderFactory : BaseFactory { - private static Dictionary _dictTextCoderTypes = null; - - private static Dictionary GetDict() - { - if (_dictTextCoderTypes != null) - { - return _dictTextCoderTypes; - } - - Type iTextCoder = typeof(ITextCoder); - IEnumerable toolFormTypes = ReflectionUtils.GetTypesOfInterface(iTextCoder); - _dictTextCoderTypes = toolFormTypes.ToDictionary(t => - { - ITextCoder textCoder = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(t) as ITextCoder; - return textCoder.Name; - }); - - return _dictTextCoderTypes; - } - - public static string[] GetSupportedCoders() - { - Dictionary dict = GetDict(); - return dict.Select(p => p.Key).ToArray(); - } - - public static ITextCoder CreateFromName(string name) - { - Dictionary dict = GetDict(); - if (dict.ContainsKey(name) == false) - { - throw new NotImplementedException(string.Format("Cant create ITextCoder with this name: {0}", name)); - } - Type textCoder = dict[name]; - return Activator.CreateInstance(textCoder) as ITextCoder; - } } } diff --git a/VAR.Toolbox/UI/Tools/FrmCoder.cs b/VAR.Toolbox/UI/Tools/FrmCoder.cs index 3e90678..85ce1b5 100644 --- a/VAR.Toolbox/UI/Tools/FrmCoder.cs +++ b/VAR.Toolbox/UI/Tools/FrmCoder.cs @@ -14,7 +14,7 @@ namespace VAR.Toolbox.UI { InitializeComponent(); - cboCode.Items.AddRange(TextCoderFactory.GetSupportedCoders()); + cboCode.Items.AddRange(TextCoderFactory.GetNames()); cboCode.SelectedIndex = 1; } diff --git a/VAR.Toolbox/UI/Tools/FrmProxyCmd.cs b/VAR.Toolbox/UI/Tools/FrmProxyCmd.cs index f88166f..b68d7ac 100644 --- a/VAR.Toolbox/UI/Tools/FrmProxyCmd.cs +++ b/VAR.Toolbox/UI/Tools/FrmProxyCmd.cs @@ -129,6 +129,10 @@ namespace VAR.Toolbox.UI if (_proxyCmdExecutor == null) { _proxyCmdExecutor = ProxyCmdExecutorFactory.CreateFromConfig(GetCurrentConfig()); + if (_proxyCmdExecutor == null) + { + _proxyCmdExecutor = new ProxyCmdExecutorDummy(string.Empty); + } } } private void CleanProxyCmdExecutor() diff --git a/VAR.Toolbox/VAR.Toolbox.csproj b/VAR.Toolbox/VAR.Toolbox.csproj index e1e0db1..d3de94c 100644 --- a/VAR.Toolbox/VAR.Toolbox.csproj +++ b/VAR.Toolbox/VAR.Toolbox.csproj @@ -73,6 +73,7 @@ Code\Json\ParserContext.cs +