How to overload generic C# methods in Factory?

Viewed 388

I want to create something like a factory, but it is not an implementation of the factory pattern.

I have an IServiceFactory interface:

public interface IServiceFactory
{
    TServiceInterface CreateService<TServiceInterface>(string url);
}

I want to create into my implementation two different services which implement two different interfaces. Let think I have:

public class FirstService : IFirstService {}
public class SecondService : ISecondService {}

public class ServiceFactory : IServiceFactory
{
    public IFirstService CreateService<IFirstService>(string url)
    {
        // logic of creation
    }

    public ISecondService CreateService<ISecondService>(string url)
    {
        // Unforchantly, it's not an overloaded method
    }
}

How to create the SecondService in this way? I know that my question and my code example is terrible and this is an improper approach, but want to know what other developers do in this case. Thanks.

3 Answers
Related