From 1e21d19fccc7a36000c192a362200e47329b81f3 Mon Sep 17 00:00:00 2001 From: Nathan Hollis Date: Tue, 4 Feb 2020 12:52:42 -0600 Subject: [PATCH] Implement GetExchangeAPIFromClassName --- .../API/Exchanges/_Base/ExchangeAPI.cs | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs b/src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs index 16e66df7..14ff7c02 100644 --- a/src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs +++ b/src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs @@ -46,6 +46,8 @@ public abstract partial class ExchangeAPI : BaseAPI, IExchangeAPI private static readonly Dictionary apis = new Dictionary(StringComparer.OrdinalIgnoreCase); private bool disposed; + private static readonly Dictionary classNamesToApiName = new Dictionary(); + #endregion Private methods #region API Implementation @@ -220,7 +222,8 @@ static ExchangeAPI() { api.Dispose(); apis[api.Name] = null; - } + classNamesToApiName[type.Name] = api.Name; + } // in case derived class is accessed first, check for existance of key if (!ExchangeGlobalCurrencyReplacements.ContainsKey(type)) @@ -281,12 +284,20 @@ public void Dispose() } } - /// - /// Get an exchange API given an exchange name (see ExchangeName class) - /// - /// Exchange name - /// Exchange API or null if not found - public static IExchangeAPI? GetExchangeAPI(string exchangeName) + public static IExchangeAPI? GetExchangeAPIFromClassName(string className) + { + if (!classNamesToApiName.TryGetValue(className, out string exchangeName)) + throw new ArgumentException("No API available with class name " + className); + + return GetExchangeAPI(exchangeName); + } + + /// + /// Get an exchange API given an exchange name (see ExchangeName class) + /// + /// Exchange name + /// Exchange API or null if not found + public static IExchangeAPI? GetExchangeAPI(string exchangeName) { // note: this method will be slightly slow (milliseconds) the first time it is called and misses the cache // subsequent calls with cache hits will be nanoseconds