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 7446691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:23:07+00:00 2026-05-29T12:23:07+00:00

I have one interface InterfaceBase and some interfaces derived from it Interface1, Interface2 .

  • 0

I have one interface InterfaceBase and some interfaces derived from it Interface1, Interface2. Next I have classes that are implementing the InterfaceX interfaces, not the base one.

Now, i am beginner in generics and so many new approaches in this made great mess in my head 🙁 . I want to create factory (static class) where I call something like

Interface1 concrete1 = Factory.Get<Interface1>();

Here is my (sample) implementation of factory, that does not work:

  public static class Factory {

    public static T Get<T>() where T: InterfaceBase{

      Type type = typeof(T);

      //return new Concrete1() as T; // type T cannot be used with the as
      //return new Concrete1() as type; //type not found
      //return new Concrete1(); // cannot implicitly convert
      //return new Concrete1() as InterfaceBase; //cannot convert IBase to T
      //return new Concrete1() as Interface1; //cannot convert Interface1 to T
    }
  }

What I want to achieve is hide the classes (they are webservice handlers) from the rest of the application to exchange them lightly. I wanted use the factory as the classes will be singletons and they will be stored in Dictionary inside the factory, so the factory can spread them across the application through this method, but as interfaces..
Maybe i am not using the constraints correctly
am I doing smthing wrong? is my approach bad? can be there something better, maybe the whole architecture shoul be reworked? diagram to show better the architecture. The factory is not in it

  • 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-29T12:23:08+00:00Added an answer on May 29, 2026 at 12:23 pm

    Methinks what you are looking for is a “Poor-Man Dependency Injection”. I guess you should use a real IoC container for that, there are a lot of options (Unity, Castle Windsor, Ninject…).

    But anyway, If you insist in doing it by yourself, go with what @Sergey Kudriavtsev is recomending. Just make sure that, for each interface, you return the proper concrete class. Something like this:

    public interface InterfaceBase { }
    public interface Interface1 : InterfaceBase { }
    public interface InterfaceX : InterfaceBase { }
    
    public class Concrete1 : Interface1 { }
    public class ConcreteX : InterfaceX { }
    
    public static class Factory
    {
        public static T Get<T>()
            where T : InterfaceBase
        {
            if (typeof(Interface1).IsAssignableFrom(typeof(T)))
            {
                return (T)(InterfaceBase)new Concrete1();
            }
            // ...
            else if (typeof(InterfaceX).IsAssignableFrom(typeof(T)))
            {
                return (T)(InterfaceBase)new ConcreteX();
            }
    
            throw new ArgumentException("Invalid type " + typeof(T).Name, "T"); // Avoids "not all code paths return a value".
        }
    }
    

    And you call it by passing the interface reference to the factory:

    var instance = factory.Get<Interface1>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one main interface and an abstract class implementing all derivable methods (that
I have one interface IMyInterface. A set of classes that implements this interface :
I have one interface that contains four functions. I have about 20 classes that
If I have a class that needs to implement an interface but one or
I have a very thin interface that's going to define one method. Should I
I have an interface that has two toolbars, one attached to the frame and
interfaces provide a useful abstraction capability. One can have a class Foo implement some
In Perl/Tk I have designed one interface in that I have one frame. That
I have an Interface called as IObSrc, I have few classes that implement this
I'm developing a plugin interface and I would like to have one method that

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.