using CommonLib.Enum;
using CommonLib.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ExchangeExecutor
{
public class ExchangeServiceController
{
private static readonly Lazy<ExchangeServiceController> lazy = new Lazy<ExchangeServiceController>(() => new ExchangeServiceController());
public static ExchangeServiceController Instance { get { return lazy.Value; } }
public string ApiAccount { get; private set; }
private ExchangeServiceController()
{ }
public ExchangeServiceBase ExchangeServiceInstance { get; private set; }
public ExchangeServiceBase SetExchangeInstance(string exchange, string key, string secret)
{
ApiAccount = CommonUtil.GetMd5HashedString(key + secret);
string instName = string.Format($"ExchangeExecutor.ExchangeService.{exchange}Service");
Type typename = Type.GetType(instName);
if (typename == null)
return null;
var property = typename.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static);
var instance = property.GetValue(null);
// CAREFUL
// As this method uses reflection to dynamically find the correct singleton class to instantiate.
// It expects the name to be in the form [ExchangeNameService] so a class with that name MUST exist prior to execution.
ExchangeServiceInstance = (ExchangeServiceBase)instance;
ExchangeServiceInstance?.Init(key, secret);
return ExchangeServiceInstance;
}
}
}