'Generic Activator.CreateInstance with different parameters
I have the following factory class:
public class ApiClientFactory
{
private readonly IProxyManager _proxyManager;
public ApiClientFactory(IProxyManager proxyManager)
{
_proxyManager = proxyManager;
}
public IAPIClient CreateApiClient(Type t)
{
try
{
IAPIClient apiClient = (IAPIClient) Activator.CreateInstance(t, _proxyManager);
return apiClient;
}
catch (Exception ex)
{
throw;
}
}
}
I also have classes defined such as:
public class DemoApiClient : IAPIClient
{
public DemoApiClient(IProxyManager proxyManager, DeviceInformation deviceInformation)
{
///
}
public IProxyManager ProxyManager { get; }
public IHTTPClient HttpClient { get; }
}
public class DemoApiClient2 : IAPIClient
{
public DemoApiClient2(IProxyManager proxyManager)
{
///
}
public IProxyManager ProxyManager { get; }
public IHTTPClient HttpClient { get; }
}
How can I rework my factory class to be generic, so that it can accept constructor parameters for different classes? i.e. DemoApiClient constructor needs IProxyManager AND DeviceInformation, however DemoApiClient2 only needs IProxyManager.
Note that these are only two examples, there might be situations where there are no parameters required, and other situations where there might more than what I've listed here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
