Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 940819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:01:05+00:00 2026-05-15T22:01:05+00:00

At first sorry for my really bad title! If you have a better title

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:01:06+00:00Added an answer on May 15, 2026 at 10:01 pm
    public interface IProvider
    {
      int Number {get;set;}
    }
    
    public class Config
    {
         public int GetNumber<T>() where T : IProvider
         {
             // code to find the provider you want
             IProvider foundProvider = ProviderFactoryMethodHere(typeof(T));
    
             return foundProvider.Number;
         }
    }
    

    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.

    // Test code example
    Config configImpl = new Config();
    int numberOfProvider = configImpl.GetNumber<Provider1>();
    Assert.Equal(1, numberOfProvider);
    

    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.

    public IProvider ProviderFactoryMethodHere(Type providerRequested)
    {
        Dictionary <Type, IProvider> providerDict;
        if (providerDict == null)
        {
          // populate dictionary with providers, keyed by their type
          providerDict = new Dictionary<Type, IProvider>();
          providerDict.Add(typeof(Provider1), new Provider1());
    
          // repeat for all providers, this is pretty simple but definitely works
          // we could use other ways of holding on to your provider instances
        }
        if (providerDict.HasKey(providerRequested))
        {
            return providerDict[providerRequested];
        }
        // could throw exception here if you want to use that kind of error
        // handling, but we'll just return null for now
        return null;
    }
    

    Cheers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At first sorry my bad English. I want define routing for this url: For
first of all I am sorry for the bad description, but I really don't
This is my first time using this site so sorry for any bad formatting
Must be really dumb today - sorry in advance; anyhow have this unordered list
Sorry my title is not great, this is my first real punt at moving
First of all Sorry for the really long post, now And this is my
First, I really sorry for my poor english... Now I use iScroll to make
Sorry I couldn't really describe my problem much better in the title. I am
First of all: Sorry for my bad English! I know the title isn't the
First off, sorry for the title. I couldn't really condense what I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.