At first sorry for my really bad title! If you have a better title just change it or make a comment =)
I have this class:
public class Config
{
public Provider1 Provider1 { get; set; }
public Provider2 Provider2 { get; set; }
public Provider3 Provider3 { get; set; }
public Provider4 Provider4 { get; set; }
public Provider5 Provider5 { get; set; }
public int GetNumber(string provider)
{
...
}
}
Each provider has a Number property:
public interface IProvider
{
int Number{ get; set; }
}
In dependency of the provider I want to return the Number of the provider.
How would you do that?
I do not want to use a big switch block.
Now all of your provider classes need to implement the IProvider interface and you need to implement a factory that will return the correct provider to you given the type that you’ve requested in the generic method.
I’ll add a simple ProviderFactoryMethod example. Note that we could also make this method generic instead of passing the type as a parameter, but I’ll leave the example as is.
Cheers.