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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:49:40+00:00 2026-06-12T08:49:40+00:00

I have a situation where I have a class that accepts an instance of

  • 0

I have a situation where I have a class that accepts an instance of a certain object type in its generic type parameter. The layout is something like this:

public abstract BaseClass { ... }
public DiamondClass : BaseClass { ... }
public SilverClass : BaseClass { ... }

public Handler<T> where T : BaseClass { ... }

I want to be able to create a method to return an instance of Handler<DiamondClass> or Handler<BaseClass> without defining the type upon input. I’ve tried something along these lines:

public Handler<BaseClass> GetHandler(HandlerType type)
{
    switch(type)
    {
        case HandlerType.Diamond: return new Handler<DiamondClass>();
        case HandlerType.Silver: return new Handler<SilverClass>();
        default: throw new InvalidOperationException("...");
    }
}

But this won’t work, because apparently Handler<DiamondClass> won’t cast implicitly to Handler<BaseClass>. I can specify it like this:

public Handler<T> GetHandler<T>(HandlerType type) where T : BaseClass
{
    switch(type)
    {
        case HandlerType.Diamond: return (Handler<T>)new Handler<DiamondClass>();
        case HandlerType.Silver: return (Handler<T>)new Handler<SilverClass>();
        default: throw new InvalidOperationException("...");
    }
}

But now I need to call GetHandler<DiamondClass> or GetHandler<BaseClass>. And that defeats the purpose of having a method that returns the proper handler based on an enum, without knowing the type. I hoped that I could define a Type object and pass it, as such:

 Type objType = typeof(DiamondClass);
 var handler = Handler<objType>();

But apparently C# won’t allow that kind of foolishness. I’ve gone about this several different ways, and I’d like to think there’s a way to do it, but I’m stumped.


(I actually did get this working by returning a dynamic object, but I’d like to avoid it if at all possible, as it loses any type safety and Intellisense support.)

  • 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-06-12T08:49:42+00:00Added an answer on June 12, 2026 at 8:49 am

    This is where co-variance comes into play, covariance and contra-variance just work only on interface and delegate, so, to solve your problem, just define a new interface IHandler as co-variant with out which specifies that the type parameter is co-variant:

    public interface IHandler<out T> where T : BaseClass 
    {
    }
    

    An interface that has a covariant type parameter enables its methods to return more derived types than those specified by the type parameter

    It will work. More information is here

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

Sidebar

Related Questions

Here's the situation. I have a base class which implements a method that accepts
I have a situation where I have an instanced class that needs to call
I have that situation: I have models Item, Region and Country. class Item(models.Model): name
I have an interesting situation that EMF forced me into: abstract class AbstractDog{ ...
i have situation like this: class IData { virtual void get() = 0; virtual
I have a situation where I need to programmatically set something that has a
I have the following situation. I have a Java Class that inherits from another
I have a situation where I have a User that is a certain profession,
I have a situation where I have a class that is referenced by several
In my current situation I have a class that performs an operation synchronously. During

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.