Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added ExchangeAPI.GetExchangeAPIFromClassName() #518

Merged
merged 2 commits into from
Feb 5, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/ExchangeSharp/API/Exchanges/_Base/ExchangeAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public abstract partial class ExchangeAPI : BaseAPI, IExchangeAPI
private static readonly Dictionary<string, IExchangeAPI?> apis = new Dictionary<string, IExchangeAPI?>(StringComparer.OrdinalIgnoreCase);
private bool disposed;

private static readonly Dictionary<string, string> classNamesToApiName = new Dictionary<string, string>();

#endregion Private methods

#region API Implementation
Expand Down Expand Up @@ -230,7 +232,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))
Expand Down Expand Up @@ -291,12 +294,20 @@ public void Dispose()
}
}

/// <summary>
/// Get an exchange API given an exchange name (see ExchangeName class)
/// </summary>
/// <param name="exchangeName">Exchange name</param>
/// <returns>Exchange API or null if not found</returns>
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);
}

/// <summary>
/// Get an exchange API given an exchange name (see ExchangeName class)
/// </summary>
/// <param name="exchangeName">Exchange name</param>
/// <returns>Exchange API or null if not found</returns>
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
Expand Down