Skip to content

Commit 8a94c43

Browse files
authored
Implement GetExchangeAPIFromClassName (#518)
1 parent 5ea3cb6 commit 8a94c43

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public abstract partial class ExchangeAPI : BaseAPI, IExchangeAPI
4646
private static readonly Dictionary<string, IExchangeAPI?> apis = new Dictionary<string, IExchangeAPI?>(StringComparer.OrdinalIgnoreCase);
4747
private bool disposed;
4848

49+
private static readonly Dictionary<string, string> classNamesToApiName = new Dictionary<string, string>();
50+
4951
#endregion Private methods
5052

5153
#region API Implementation
@@ -230,7 +232,8 @@ static ExchangeAPI()
230232
{
231233
api.Dispose();
232234
apis[api.Name] = null;
233-
}
235+
classNamesToApiName[type.Name] = api.Name;
236+
}
234237

235238
// in case derived class is accessed first, check for existance of key
236239
if (!ExchangeGlobalCurrencyReplacements.ContainsKey(type))
@@ -291,12 +294,20 @@ public void Dispose()
291294
}
292295
}
293296

294-
/// <summary>
295-
/// Get an exchange API given an exchange name (see ExchangeName class)
296-
/// </summary>
297-
/// <param name="exchangeName">Exchange name</param>
298-
/// <returns>Exchange API or null if not found</returns>
299-
public static IExchangeAPI? GetExchangeAPI(string exchangeName)
297+
public static IExchangeAPI? GetExchangeAPIFromClassName(string className)
298+
{
299+
if (!classNamesToApiName.TryGetValue(className, out string exchangeName))
300+
throw new ArgumentException("No API available with class name " + className);
301+
302+
return GetExchangeAPI(exchangeName);
303+
}
304+
305+
/// <summary>
306+
/// Get an exchange API given an exchange name (see ExchangeName class)
307+
/// </summary>
308+
/// <param name="exchangeName">Exchange name</param>
309+
/// <returns>Exchange API or null if not found</returns>
310+
public static IExchangeAPI? GetExchangeAPI(string exchangeName)
300311
{
301312
// note: this method will be slightly slow (milliseconds) the first time it is called and misses the cache
302313
// subsequent calls with cache hits will be nanoseconds

0 commit comments

Comments
 (0)